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?

4

2 回答 2

3

您必须person像这样访问您的关系:

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

要了解有关查询生成器和联合表的更多信息:

  • 这个SO帖子。
于 2013-01-28T11:35:17.110 回答
1

尝试

$authQB = $this->createQueryBuilder( 'a' )
    ->select('a')
    ->innerJoin('a.person', 'p')
    ->where('p.firstname LIKE :myStuff')
    ->setParameter('myStuff', '%'.$q.'%');
于 2013-01-28T11:04:34.830 回答