在Symfony2中爆炸实体对象

时间:2013-11-18 16:30:08

标签: php symfony doctrine-orm twig

我正在尝试在Symfony2 Web App中创建图库。

每个帖子可能有也可能没有画廊。我的画廊是Post实体/类下的文本映射类型:

#Post.orm.yml

MyProject\MyProjectBundle\Entity\Post:
type: entity
table: post
repositoryClass: MyProject\MyProjectBundle\Entity\PostRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        # ...
        gallery:
            type: text
            nullable: true
        #...

由于图库中有很多图像,我认为通过数据夹具用逗号分隔每个图像是有意义的:

image1.png, image2.jpg, examplename-3rdimage.gif, 4thandfinal.jpg

但是,我希望画廊在观看时输出如下:

<li>image1.png</li>
<li>image2.jpg</li>
<li>examplename-3rdimage.gif</li>
<li>4thandfinal.jpg</li>

我的控制器然后调用Post Entity:

/* PostController.php */

public function postshowAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('MPMPBBundle:Post')->find($id);

    $gallery = $em->getRepository('MPMPBBundle:Post')->getGallery();


    if (!$entities) {
        throw $this->createNotFoundException('Unable to find Post entity.');
    }

    return $this->render('MPMPBBundle:Post:postshow.html.twig', array(
        'entities'      => $entities,
        'gallery'       => $gallery
    ));
}

您可能已经注意到,我从我的存储库类中引用了函数:getGallery()PostRepository

/* PostRepository.php */

class PostRepository extends EntityRepository
{
    public function getGallery()
    {
        $postGallery = $this->createQueryBuilder('e')
                            ->select('e.gallery')
                            ->getQuery()
                            ->getResult();

        $gallery = array();
        foreach ($postGallery as $postGal)
        {
            $gallery = array_merge(explode(",", $postGal['gallery']), $gallery);
        }

        foreach ($gallery as &$gal)
        {
            $gal = trim($gal);
        }

        return $gallery;
    }
}

最后,我的twig文件:postshow.html.twig,如下所示:

{% for gallery in gallery %}
    <li>{{ gallery }}</li>
{% endfor %}

澄清一下,我希望实现的目标是:

# mysite.com/post/post-1

<li>image1.png</li>
<li>image2.jpg</li>
<li>examplename-3rdimage.gif</li>
<li>4thandfinal.jpg</li>

# mysite.com/post/post-2

<li>image5.png</li>
<li>image11.jpg</li>
<li>examplename-18thimage.gif</li>
<li>22ndandfinal.jpg</li>

每个帖子都会显示相应的图库。

根据上面所写的内容,所取得的成果是每个Gallery的所有Post个项目都会输出,而我只需要个别帖子的图库项目:

# mysite.com/post/post-1

<li>image1.png</li>
<li>image2.jpg</li>
<li>examplename-3rdimage.gif</li>
<li>4thandfinal.jpg</li>
<li>image5.png</li>
<li>image11.jpg</li>
<li>examplename-18thimage.gif</li>
<li>22ndandfinal.jpg</li>

# mysite.com/post/post-2

<li>image1.png</li>
<li>image2.jpg</li>
<li>examplename-3rdimage.gif</li>
<li>4thandfinal.jpg</li>
<li>image5.png</li>
<li>image11.jpg</li>
<li>examplename-18thimage.gif</li>
<li>22ndandfinal.jpg</li>

1 个答案:

答案 0 :(得分:2)

gallery字段位于Post表中,因此您无需再次在数据库中进行查询,只需从当前实体获取galleryexplode()就像这样:

/* PostController.php */

public function postshowAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('MPMPBBundle:Post')->find($id);

    $gallery = null;
    if (null !== $entities->getGallery())
    {
        $gallery = explode(",", $entities->getGallery());
        $gallery = array_map("trim", $gallery);
    }

    if (!$entities) {
        throw $this->createNotFoundException('Unable to find Post entity.');
    }

    return $this->render('MPMPBBundle:Post:postshow.html.twig', array(
        'entities'      => $entities,
        'gallery'       => $gallery
    ));
}