1

I am using Symfony 3.2. I have an entity called Site which contains a OneToMany relationship to another entity called Indication.

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. I only get an ArrayCollection which I don't know what to do with.

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

I also tried to use the queryBuilder like in this answer.

$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.

1 Answers1

4

Seems like this can be solved by SQL by doing a simple join, something like this should work:

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