【问题标题】:Symfony2 ORM, entity manyToOne and oneToMany. doesn't work querySymfony2 ORM,实体 manyToOne 和 oneToMany。不工作查询
【发布时间】:2023-03-11 21:02:02
【问题描述】:

我有一个问题。 我不会通过我的帖子 ID 返回 cmets,但是当我这样做时,我有这个错误:

[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 

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

最后一个代码是,我返回帖子的方法和 cmets:

    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
    ));
}

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

【问题讨论】:

    标签: php symfony orm doctrine-orm


    【解决方案1】:

    您应该能够通过使用JOIN 方法检索cmets 来执行类似的操作。

    YourBundle/Entity/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();
    

    我希望我不会在这个代码示例中出错。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多