教义 2.3 标准.访问相关对象 [英] Doctrine 2.3 Criteria. Accessing a related Object

查看:14
本文介绍了教义 2.3 标准.访问相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 设置标准教义文件.

不幸的是,他们没有告诉您如何访问相关对象的属性.我给你举个例子.

Unfortunately they don't tell you how to access attributes of an related Object. Let me give you an example.

我有一个 ArrayCollection 产品.每个产品都有一个类别.我想过滤 ArrayCollection 的类别名称.现在我正在尝试按如下方式设置标准:

I have an ArrayCollection of Products. Every Product has a Category. I want to filter the ArrayCollection for a Category Name. Now I am trying to set up a Criteria as follows:

$criteria = Criteria::create()
  ->where(Criteria::expr()->eq("category.name", "SomeCategoryName"));

现在我得到以下异常:

An exception has been thrown during the rendering of a template ("Unrecognized field: category.name")

如何访问相关对象?

推荐答案

我查看了源代码 Criteria::expr()->eq("name", --- second value ---).第二个值需要 DoctrineCommonCollectionsExprValue 的实例.因此,不可能在其中放置另一个 Exprcriteria.只有 Expr AndOr 需要另一个 Expr.我很确定你想用其他函数来解决这个问题,比如 filter() 或者用 getIterator() 获取迭代器.这是如何使用 filter() 方法完成的.

I looked into the source code Criteria::expr()->eq("name", --- second value ---). Second value expects an instance of DoctrineCommonCollectionsExprValue. So it's not possible to put another Expr or criteria in there. Only the Expr And and Or take another Expr. I'm pretty sure you are suppose to solve this with other functions like filter() or get an iterator with getIterator(). This is how it can be done with the filter() method.

$filteredProducts = 
    $products->filter(function($key, $element) use ($categoryName) {
        return $element->getCategory()->getName() === categoryName;
    });

如果您可以为每个下一个关系使用 Iterator,您可以嵌套 foreach 循环并在其中进行过滤.

If you can an Iterator for each next relation you can nest foreach loops and filter inside those.

这篇关于教义 2.3 标准.访问相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆