1

I have 2 entities: author and person.

In the author entity, there is a person field which is in fact the person object:

/**
 * @ORM\ManyToOne(targetEntity="Person", inversedBy="submission_authors")
 * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
 */
protected $person;

Now, in the repository: AuthorRepository, I would like to search for some authors by their firstname. To do this, I need to access the person object for the corresponding author ans look on the person's firstname.

I tryed:

 public function searchAuthors($q)
{
    $authQB = $this->createQueryBuilder( 'a' )
    ->select('a')
    ->where("a.person.firstname LIKE '%".$q."%'");

    return $authQB->getQuery()->getResult();
}

But the problem is that I am geting an error:

 [Syntax Error] line 0, col 78: Error: Expected Doctrine\ORM\Query\Lexer::T_LIKE, got '.' 

Coud you please help me on how to resolve that?

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

2 Answers2

3

You'd have to access your person relation like this:

$authQB = $this->createQueryBuilder( 'a' )
    ->select('a')
    ->leftJoin('a.person', 'p')
   //...

To learn a bit more about query builder and joint tables:

  • This SO post.
Community
  • 1
  • 1
Mick
  • 30,759
  • 16
  • 111
  • 130
1

Try

$authQB = $this->createQueryBuilder( 'a' )
    ->select('a')
    ->innerJoin('a.person', 'p')
    ->where('p.firstname LIKE :myStuff')
    ->setParameter('myStuff', '%'.$q.'%');
Geoffrey Brier
  • 789
  • 6
  • 18
  • Thanks, but the problem is not there. Igf you take a look on thee error message from symfony, doctrine is not expecting a dot (".") after the a.person. There should be another possibility to access the person object. – Milos Cuculovic Jan 28 '13 at 11:07
  • You're right, I've updated my answer. I don't think that we can, in doctrine, wright things like `rootAlias.relationship.field`, you actually have make an inner/left join. – Geoffrey Brier Jan 28 '13 at 13:01
  • Yes, that's what @Patt suggested me. Thank you – Milos Cuculovic Jan 28 '13 at 13:05