数组 属性 的等效 IN 子句
IN clause equivalent for array property
实际上我有两个实体 A 和 B,由多对多关系映射。
Doctrine 用 DQL 可以访问的 A.bElements 和 B.aElements 属性表示这种关系,所以我想在我的查询中使用这些属性,因为这个查询包含其他 where 子句,并完成所有工作同样的查询看起来很好。
我有一个包含 returns 一些 B 元素的子查询,我想检查 A.bElements 中是否至少存在这些元素之一。问题是 IN 子句仅适用于一个值,不适用于数组。
这是我的查询示例(这是针对搜索引擎的):
$qb->select('l')
->from('AppBundle:Livre', 'l')
->where("l.ISBN LIKE :motcle")
->orWhere("l.cote LIKE :motcle")
->orWhere($qb->expr()->in('l.dessinateurs',
$em->createQueryBuilder()
->select('d.id')
->from('AppBundle:Artiste', 'd')
->where('d.dessinateur=1')
->andWhere('d.nom LIKE :motcle OR d.prenom LIKE :motcle')
->setParameter('motcle', '%'.$motcle.'%')
->getDql()
))
->setParameter('motcle', '%'.$motcle.'%');
这里我想要所有 'Livre' 实体,它们在 l.dessinateurs
属性.
中至少有一个子查询结果
我知道可以通过分别检索所有 B 和 A 元素来实现这项工作,然后使用 php 检查是否存在公共元素。也可以使用连接 table,但我猜 Doctrine 旨在避免直接使用这样的 table。
也许有人可以帮助我?也许问题还不够清楚,如果有必要,我可以尝试重新表述。提前致谢。
编辑:
以下查询工作正常:
$qb->select('l')
->from('AppBundle:Livre', 'l')
->join('l.serie', 's')
->join('l.editeur', 'e')
->join('l.dessinateurs', 'd', 'WITH',
'd.nom LIKE :motcle
OR d.prenom LIKE :motcle
OR l.ISBN LIKE :motcle
OR l.cote LIKE :motcle
OR l.etat LIKE :motcle
OR l.edition LIKE :motcle
OR s.libelle LIKE :motcle
OR e.nom LIKE :motcle')
->join('l.auteurs', 'a', 'WITH',
'a.nom LIKE :motcle
OR a.prenom LIKE :motcle
OR l.ISBN LIKE :motcle
OR l.cote LIKE :motcle
OR l.etat LIKE :motcle
OR l.edition LIKE :motcle
OR s.libelle LIKE :motcle
OR e.nom LIKE :motcle')
->setParameter('motcle', '%'.$motcle.'%');
不幸的是,我必须在两个连接中重复其他 LIKE 子句,但这是另一个问题。下面 ccKep 的回答是完美的。
你可以试一试:
$qb->select('l')
->from('AppBundle:Livre', 'l')
->join('l.dessinateurs', 'd', 'WITH', 'd.dessinateur=1 AND (d.nom LIKE :motcle OR d.prenom LIKE :motcle)')
->where("l.ISBN LIKE :motcle")
->orWhere("l.cote LIKE :motcle")
->setParameter('motcle', '%'.$motcle.'%');
实际上我有两个实体 A 和 B,由多对多关系映射。 Doctrine 用 DQL 可以访问的 A.bElements 和 B.aElements 属性表示这种关系,所以我想在我的查询中使用这些属性,因为这个查询包含其他 where 子句,并完成所有工作同样的查询看起来很好。
我有一个包含 returns 一些 B 元素的子查询,我想检查 A.bElements 中是否至少存在这些元素之一。问题是 IN 子句仅适用于一个值,不适用于数组。
这是我的查询示例(这是针对搜索引擎的):
$qb->select('l')
->from('AppBundle:Livre', 'l')
->where("l.ISBN LIKE :motcle")
->orWhere("l.cote LIKE :motcle")
->orWhere($qb->expr()->in('l.dessinateurs',
$em->createQueryBuilder()
->select('d.id')
->from('AppBundle:Artiste', 'd')
->where('d.dessinateur=1')
->andWhere('d.nom LIKE :motcle OR d.prenom LIKE :motcle')
->setParameter('motcle', '%'.$motcle.'%')
->getDql()
))
->setParameter('motcle', '%'.$motcle.'%');
这里我想要所有 'Livre' 实体,它们在 l.dessinateurs
属性.
我知道可以通过分别检索所有 B 和 A 元素来实现这项工作,然后使用 php 检查是否存在公共元素。也可以使用连接 table,但我猜 Doctrine 旨在避免直接使用这样的 table。
也许有人可以帮助我?也许问题还不够清楚,如果有必要,我可以尝试重新表述。提前致谢。
编辑:
以下查询工作正常:
$qb->select('l')
->from('AppBundle:Livre', 'l')
->join('l.serie', 's')
->join('l.editeur', 'e')
->join('l.dessinateurs', 'd', 'WITH',
'd.nom LIKE :motcle
OR d.prenom LIKE :motcle
OR l.ISBN LIKE :motcle
OR l.cote LIKE :motcle
OR l.etat LIKE :motcle
OR l.edition LIKE :motcle
OR s.libelle LIKE :motcle
OR e.nom LIKE :motcle')
->join('l.auteurs', 'a', 'WITH',
'a.nom LIKE :motcle
OR a.prenom LIKE :motcle
OR l.ISBN LIKE :motcle
OR l.cote LIKE :motcle
OR l.etat LIKE :motcle
OR l.edition LIKE :motcle
OR s.libelle LIKE :motcle
OR e.nom LIKE :motcle')
->setParameter('motcle', '%'.$motcle.'%');
不幸的是,我必须在两个连接中重复其他 LIKE 子句,但这是另一个问题。下面 ccKep 的回答是完美的。
你可以试一试:
$qb->select('l')
->from('AppBundle:Livre', 'l')
->join('l.dessinateurs', 'd', 'WITH', 'd.dessinateur=1 AND (d.nom LIKE :motcle OR d.prenom LIKE :motcle)')
->where("l.ISBN LIKE :motcle")
->orWhere("l.cote LIKE :motcle")
->setParameter('motcle', '%'.$motcle.'%');