我正在尝试用symfony制作主题/评论系统,我对doctrine的querybuilder有一些疑问。
如果我有两个实体连接:
/**
* @ORM'ManyToOne(targetEntity="Topicit", inversedBy="comments")
* @ORM'JoinColumn(name="topic_id", referencedColumnName="id")
*/
protected $topicit;
和
/**
* @ORM'OneToMany(targetEntity="Comment", mappedBy="topicit")
*/
protected $comments;
目前我有构建器(获取每一列):
$repository = $this -> getDoctrine() -> getRepository('FTFairyBundle:Comment');
$query = $repository->createQueryBuilder('p')
->orderBy('p.topicit', 'ASC')
->getQuery();
$comment = $query ->getResult();
如何使它只得到想要的topic_id
?比如我打开一个id为#1的页面我想让评论连接到这个id。
感谢您的宝贵时间
对于这种任务,您不需要使用查询生成器,您可以简单地在您的Topicit
实体中使用getter(当然您需要从DB检索)
类似->getComments()
*(当然该方法需要在Topic
类中定义)
原则将只关心负载&"一致&";Topic
对象中的数据
方法定义
public getComments()
{
return $this->comments;
}
如果你想直接查询,你可以修改你的代码如下
$topicit_id = ... //code to retrieve topicit_id
$repository = $this->getDoctrine()-> getRepository('FTFairyBundle:Comment');
$query = $repository->createQueryBuilder('p')
->where('p.topicit = :topicit_id')
->setParameter('topicit_id', $topicit_id)
->orderBy('p.topicit', 'ASC')
->getQuery();
$comment = $query ->getResult();