在Symfony中没有找到其医生的一对多孩子

时间:2016-09-19 22:19:22

标签: symfony doctrine-orm

我在Symfony2中与医生建立了一对多的关系。父母是广告,孩子是图像。这很好用。现在我想得到一个孩子,所以我可以删除/更改它。

我希望通过以下代码获取子ID:

        $image = $this->getDoctrine()
        ->getRepository('MyBundle:Image')
        ->findOneBy(
            array('id' => $id)
        );

只有它不会返回任何内容。如果我删除与其父级的关联,则会找到相同的代码。

怎样才能找到孩子?或者这只能由其父母来实现?

2 个答案:

答案 0 :(得分:0)

也许DQL查询可能会破坏繁文缛节?

    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->createQuery('SELECT i FROM MyBundle:Image i WHERE id = :id')
        ->setParameter('id', $id)
        ->getResult();

答案 1 :(得分:0)

嗯,也许我不是很了解,但是如果你想通过它的id检索一个图像,它应该更简单:

$em = $this->getDoctrine()->getManager();
$image = ->getRepository('MyBundle:Image')->find($id);

其中$ id是图片ID,不是吗?