0

I'm facing an issue while searching through several entities with Doctrine2.

I have an entity People joined to another entity Email on a "OneToMany" relationship :

<?php
class People
{
  // ...
  /**
   * @ORM\OneToMany(targetEntity="Email", mappedBy="people")
   */
  protected $emails;
  // ...
}

class Email
{
  // ...
  /**
   * @ORM\ManyToOne(targetEntity="People", inversedBy="emails")
   * @ORM\JoinColumn(name="id", referencedColumnName="id")
   */
  protected $people;
}

In my PeopleRepository, I try to do something like this :

$queryBuilder = $this->_em->createQueryBuilder()
                  ->select('p')
                  ->from('MyBundle:People', 'p')
                  ->leftJoin('p.emails', 'e')
                  ->where('p.name LIKE :name')
                  ->andWhere('p.firstName LIKE :firstName')
                  ->andWhere('e.email LIKE :email')
                  ->setParameter('name', '%'.$name.'%')
                  ->setParameter('firstName', '%'.$firstName.'%')
                  ->setParameter('email', '%'.$email.'%');

But the email condition doesn't seem to be interpreted. I would like to get a response with the People entity, corresponding to the email attribute from the Email entity it's linked to.

I don't know if what I say is clear enough.

But if anyone understood my issue, do know how I could do ?

Thank you :)

Pierre Rolland
  • 183
  • 1
  • 4

1 Answers1

0

From what I see of your code, you are doing a left join

->from('MyBundle:People', 'p')
->leftJoin('p.emails', 'e')

so it will returns ALL the people entity filtering only on

->where('p.name LIKE :name')
->andWhere('p.firstName LIKE :firstName')

replace your leftJoin with a innerJoin should fix your issue.

Best regards, Christophe

user1041440
  • 191
  • 1
  • 7