简体   繁体   English

在symfony /主义中查询非空的OneToMany关系

[英]query for not null OneToMany relationship in symfony / doctrine

I am using Symfony 3.2. 我正在使用Symfony 3.2。 I have an entity called Site which contains a OneToMany relationship to another entity called Indication . 我有一个名为Site的实体,其中包含与另一个名为Indication实体的OneToMany关系。

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;
}

I want to query for all sites where Indications are available. 我想查询所有可用Indications sites I only get an ArrayCollection which I don't know what to do with. 我只得到一个不知道该怎么做的ArrayCollection。

$sites = $repository->findAll();
foreach ($sites as $site) {
   $site->getIndications();
}

I also tried to use the queryBuilder like in this answer . 我也尝试在此答案中使用queryBuilder。

$query = $repository->createQueryBuilder('s');
$result = $query->where('s.indications IS NOT NULL')
    ->getQuery()
    ->getResult();

which throws the following error: 这将引发以下错误:

[Semantical Error] line 0, col 46 near 'indications IS': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

Seems like this can be solved by SQL by doing a simple join, something like this should work: 看起来像这样可以通过执行简单的连接来用SQL来解决,类似的事情应该可以起作用:

$query = $repository->createQueryBuilder('s');
$result = $query->select('p')
    ->join('p.indications', 'i')
    ->getQuery()
    ->getResult();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM