ORM QueryBuilder with Entity subobjects

2019-06-06 04:49发布

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?

2条回答
Fickle 薄情
2楼-- · 2019-06-06 05:00

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.
查看更多
SAY GOODBYE
3楼-- · 2019-06-06 05:01

Try

$authQB = $this->createQueryBuilder( 'a' )
    ->select('a')
    ->innerJoin('a.person', 'p')
    ->where('p.firstname LIKE :myStuff')
    ->setParameter('myStuff', '%'.$q.'%');
查看更多
登录 后发表回答