本文介绍了教义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,---第二个值---)。第二个值需要 Doctrine\Common\Collections\Expr\Value 的实例。所以不可能在那里再放一个 Expr 标准。只有 Expr 采取另一个 Expr的
我确定你可以用其他函数,如 filter()来解决这个问题,或者使用 getIterator()。这样可以通过 filter()方法来实现。

I looked into the source code Criteria::expr()->eq("name", --- second value ---). Second value expects an instance of Doctrine\Common\Collections\Expr\Value. 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标准。访问相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:37