Php 在Symfony2中分解实体对象
我正在尝试在我的Symfony2 Web应用程序中创建图库 每一篇文章可能有也可能没有图库。我的图库是文本映射类型,位于Post实体/类下:Php 在Symfony2中分解实体对象,php,symfony,doctrine-orm,twig,Php,Symfony,Doctrine Orm,Twig,我正在尝试在我的Symfony2 Web应用程序中创建图库 每一篇文章可能有也可能没有图库。我的图库是文本映射类型,位于Post实体/类下: #Post.orm.yml MyProject\MyProjectBundle\Entity\Post: type: entity table: post repositoryClass: MyProject\MyProjectBundle\Entity\PostRepository id: id: typ
#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>
您可能已经注意到,我从我的存储库类中引用了函数: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;
}
}
最后,我的细枝文件:postshow.html.twig
,如下所示:
{% for gallery in gallery %}
<li>{{ gallery }}</li>
{% endfor %}
/* 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
));
}
gallery
字段位于Post
表中,因此您无需在数据库中再次查询,只需从当前实体中获取gallery
,然后按如下方式进行分解()
{% for gallery in gallery %}
<li>{{ gallery }}</li>
{% endfor %}
/* 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
));
}
我太。。。我太慢了。我确信这是显而易见的,但我不认为这是显而易见的。谢谢。我很感激。
/* 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
));
}