Doctrine 2.3 Criteria. Accessing a related Object

前端 未结 2 1868
北荒
北荒 2021-01-04 10:18

I am trying to set up a Criteria according to the Doctrine Docs.

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

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 10:33

    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;
        });
    

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

提交回复
热议问题