php - How to select all rows that don't have one-to-many entity with certain value using Doctrine -


i have 2 entities: cage , bird. cage has birds inside relationship one-to-many.

bird has field name. how can select cages there's no bird name eagle inside.

i trying this:

$cages = $this->createquerybuilder("c")             ->leftjoin("c.birds", "b")             ->where("b.name != :name")             ->setparameter("name", 'eagle')             ->getquery()->getresult(); 

this works if there's single bird (eagle) in cage. cage not selected correct behavior.

but if there multiple birds , 1 of them eagle, cage gets selected though eagle inside.

this idea, adapt table , column names necessary:

select * cages cage_id not in (select cage_id birds name='eagle'); 

so, using doctrine:

$qb = $this->createquerybuilder();  $cageswitheagles = $qb->select('b.cage_id')     ->from('birds', 'b')     ->where("b.name = :name")     ->setparameter("name", 'eagle')     ->getquery()     ->getresult();  $cageswithouteagles = $qb->select('c.cage_id')     ->from('cages', 'c')     ->where($qb->expr()->notin('c.cage_id', $cageswitheagles))     ->getquery()     ->getresult(); 

(inspired 'where not in' query doctrine query builder)


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -