我需要从2个表中获取数据.实际上只需要在表格键的基础上将第二表中的一个字段包含到第一表的结果中.
我做了研究,并找到了一种方法来做到这一点.
$qb->select(array('a', 'c'))
->from('Sdz\BlogBundle\Entity\Article', 'a')
->leftJoin('a.comments', 'c');
Run Code Online (Sandbox Code Playgroud)
当我实现它时,它显示错误错误:类没有名为comments的关联,这是显而易见的事情,因为注释不是Article表(实体)的字段.我很困惑如何定义第二个表从哪个字段需要提取,因为a.comments是Article表的关联.
要使用Doctrine's join,您必须说明Doctrine实体之间的关系。这是使用关系注释 ( ) 完成的,您可以在http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html@OneToOne, @OneToMany, @ManyToOne, @ManyToMany中找到该注释
在 Doctrine 中,您不能使用没有关联的连接。
根据您的问题,我假设您的 Article 实体OneToMany与 Comments 有关系。然后,您的实体定义应类似于:
/**
* @Entity
*/
class Article{
//.... all your current fields
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="article")
*/
private $comments;
//.... setters and getters
}
/**
* @Entity
*/
class Comment{
//.... all your current fields
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
*/
private $article;
//.... setters and getters
}
Run Code Online (Sandbox Code Playgroud)
使用这些实体您将能够执行查询。
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |