ORM QueryBuilder与实体子对象 [英] ORM QueryBuilder with Entity subobjects

查看:127
本文介绍了ORM QueryBuilder与实体子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:作者和个人。

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;

现在,在存储库:AuthorRepository中,我想以他们的名字搜索一些作者。为了做到这一点,我需要访问相应作者的个人对象并查看该人的名字。

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.

我尝试了:

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

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

但问题是我发生错误:

 [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?

推荐答案

您必须访问个人关系如下:

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

要学习更多关于查询构建器和联合表:

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

  • This SO post.

这篇关于ORM QueryBuilder与实体子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆