query for not null OneToMany relationship in symfony / doctrine
我使用的是 Symfony 3.2。我有一个名为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Site { /** * @ORM\\OneToMany(targetEntity="IhkBundle\\Entity\\Indication", mappedBy="site") */ private $indications; } class Indication { /** * @ORM\\ManyToOne(targetEntity="IhkBundle\\Entity\\Site", inversedBy="indications") * @ORM\\JoinColumn(name="site_id", referencedColumnName="id") */ private $site; } |
我想在
1 2 3 4 | $sites = $repository->findAll(); foreach ($sites as $site) { $site->getIndications(); } |
我也尝试像在这个答案中一样使用 queryBuilder。
1 2 3 4 | $query = $repository->createQueryBuilder('s'); $result = $query->where('s.indications IS NOT NULL') ->getQuery() ->getResult(); |
抛出以下错误:
1 | [Semantical Error] line 0, col 46 near 'indications IS': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected. |
似乎这可以通过SQL通过简单的连接来解决,这样的事情应该可以工作:
1 2 3 4 5 | $query = $repository->createQueryBuilder('s'); $result = $query->select('p') ->join('p.indications', 'i') ->getQuery() ->getResult(); |