我有一个数据库,它看起来像这样:
实体:发布,评论..注释具有外键post_id。
我需要通过其ID获取属于特定帖子的评论。并按getCreated()进行排序。
问题是,当我尝试通过以下post_id查找所有评论时:
$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBypost_id($postID);
usort($comments, function($a, $b){
return ($a->getCreated() < $b->getCreated());
});
Im gettig ..CommonBundle'Entity'Comment' has no field 'postId'.
可能是因为它的外键。
有什么办法,怎么做?
多谢!
似乎你有这样的关联:
/**
* @ORM'ManyToOne(targetEntity="Post")
*/
protected $post;
所以你应该通过post
找到你的评论,而不是post_id
:
$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBy(['post' => $postID]);
您也可以通过添加['created' => 'DESC']
作为第二个参数来对其进行排序:
$comments = $this->get('doctrine')->getRepository('BlogCommonBundle:Comment')->findBy(['post' => $postID], ['created' => 'DESC']);