我有一个问题。我不会通过我的帖子ID返回评论,但是当我这样做时,我有这个错误:
[Semantical Error] line 0, col 57 near 'post_id = :post_id': Error: Class Ibw'JobeetBundle'Entity'comments has no field or association named post_id
我的评论.orm.yml
Ibw'JobeetBundle'Entity'comments:
type: entity
table: null
repositoryClass: Ibw'JobeetBundle'Entity'commentsRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
comment:
type: text
addDate:
type: datetime
column: add_date
heading:
type: string
length: 255
name:
type: string
length: 255
manyToOne:
postId:
targetEntity: Blog
inversedBy: comments
joinColumn:
name: post_id
referencedColumnName: id
lifecycleCallbacks: { }
我的博客.orm.yml
Ibw'JobeetBundle'Entity'Blog:
type: entity
table: null
repositoryClass: Ibw'JobeetBundle'Entity'BlogRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
text:
type: text
created_at:
type: datetime
author:
type: string
length: 255
image:
type: string
lenght: 255
nullable: true
oneToMany:
comments:
targetEntity: comments
mappedBy: Blog
lifecycleCallbacks:
prePersist: [ preUpload, setCreatedAtValue ]
preUpdate: [ preUpload, setUpdatedAtValue ]
postPersist: [ upload ]
postUpdate: [ upload ]
postRemove: [ removeUpload ]
这是我的方法:
public function getComments($postId) {
$qb = $this->createQueryBuilder('c')
->where('c.post_id = :post_id')
->setParameter('post_id', $postId)
->orderBy('c.add_date', 'DESC');
return $qb->getQuery()->getResult();
}
最后一个代码是,方法,我返回我的帖子和评论:
public function showAction($id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('IbwJobeetBundle:Blog')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Blog entity.');
}
$comments = $em->getRepository('IbwJobeetBundle:comments')
->getComments($id);
$image = $entity->getWebsPath();
return $this->render('IbwJobeetBundle:Blog:show.html.twig', array(
'entity' => $entity,
'image' => $image,
'comments' => $comments
));
}
所以我,不知道为什么它不起作用,请帮助某人。
您应该能够通过使用 JOIN
方法来检索注释来执行类似操作。
捆绑包/实体/您的存储库.php
$qb = $this->createQueryBuilder('c')
->join('b.comments', 'b')
->where('c.post_id = :post_id')
->setParameter('c.post_id', $postId)
->orderBy('c.add_date', 'DESC');
return $qb->getQuery()->getResult();
我希望我不会在这个代码示例中犯错误。
我不知道
为什么,但错误地认为查询在我编写时开始工作而不是行名称 实体名称:)
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.postId = :id')
->setParameter('id', $blogId)
->addOrderBy('c.addDate', 'DESC');