Doctrine 2 ArrayCollection filter method

前端 未结 4 780
遥遥无期
遥遥无期 2020-12-02 10:46

Can I filter out results from an arrayCollection in Doctrine 2 while using lazy loading? For example,

// users = ArrayCollection with User entities containin         


        
4条回答
  •  心在旅途
    2020-12-02 11:02

    Doctrine now has Criteria which offers a single API for filtering collections with SQL and in PHP, depending on the context.

    https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections

    Update

    This will achieve the result in the accepted answer, without getting everything from the database.

    use Doctrine\Common\Collections\Criteria;
    
    /**
     * @ORM\Entity
     */
    class Member {
      // ...
      public function getCommentsFiltered($ids) {
        $criteria = Criteria::create()->where(Criteria::expr()->in("id", $ids));
    
        return $this->getComments()->matching($criteria); 
      }
    }
    

提交回复
热议问题