Doctrine选择了连接对象

时间:2018-02-12 22:21:22

标签: symfony doctrine-orm symfony-3.3

我有两个相关的entery(class): 头等舱(Todo班):

class Todo
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
}

评论类(部分):

class Comment
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * ID человека
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Todo")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="postid", referencedColumnName="id")
     * })
     */
    private $postId;
}

我如何通过Todo id选择评论?

在控制器中我尝试:

$todos=$this->getDoctrine()
    ->getRepository('AppBundle:Todo')
    ->find($id);
//   var_dump($todos);
$em=$this->getDoctrine()->getManager();
$comments=$em->createQueryBuilder()
    ->select('c')
    ->from('Comment','c')
    ->leftJoin('')
    ->where('postid',':postID')
    ->orderBy('postid', 'ASC')
     ->setParameter('postID', $id)
     ->getQuery()
     ->getResult();

如何选择与Todo连接的评论?

1 个答案:

答案 0 :(得分:0)

也许这些改变会做你想要的:

class Comment
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * ID человека
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Todo")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="postid", referencedColumnName="id")
     * })
     */
    private $post;
}

和...

$todos = $this->getDoctrine()
    ->getRepository('AppBundle:Todo')
    ->find($id);

$em = $this->getDoctrine()->getManager();
$comments = $em->createQueryBuilder()
    ->select('c')
    ->from('Comment', 'c')
    ->leftJoin('AppBundle:Todo', 't')
    ->where('c.post = :todo')
    ->orderBy('post', 'ASC')
    ->setParameter('todo', $todos)
    ->getQuery()
    ->getResult();