Symfony2 ORM,实体manyToOne和oneToMany。没有工作查询

时间:2014-03-31 10:57:57

标签: php symfony orm doctrine-orm

我有一个问题。 我不会通过我的帖子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: {  }

我的Blog.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
    ));
}

所以,我不知道为什么它不起作用,请帮助别人。

2 个答案:

答案 0 :(得分:0)

您应该可以使用JOIN方法检索评论,从而做到这一点。

<强> YourBundle /实体/ YourRepository.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();

我希望我不会在这段代码中犯错误。

答案 1 :(得分:0)

我不知道为什么,但是当我写的时候错误地认为查询开始工作了 而不是行名称实体名称:)

   $qb = $this->createQueryBuilder('c')
                ->select('c')
                ->where('c.postId = :id')
                ->setParameter('id', $blogId)
                ->addOrderBy('c.addDate', 'DESC');