Hello, I’m Julian

Symfony3/Doctrine: Collections in Entity filtern


Mit Doctrine ist es jetzt möglich mit "Criteria" Collections direct über SQL in einer Getter-Funktion der Entity zu filtern.

namespace ...\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
/**
 * @ORM\Entity
 */
class Parent {
    /**
     * @ORM\OneToMany(targetEntity="Children", mappedBy="parent")
     */
    private $children;
  ...
  public function getChildren($onlyactive=true) {
    if($onlyactive){
        $criteria = Criteria::create()
            ->where(Criteria::expr()->eq("isactive", true);
        return $this->getComments()->matching($criteria); 
    }else{
        return $this->getComments();
    }
  }
}

Filtering Collections auf docs.doctrine-project.org

Date: Fri, 09 Sep 2016 7:48:00

PREVIOUS

Blog Comments powered by Disqus.