Symfony 在树枝中显示对象值
Symfony displaying object value in twig
我有一个实体 Playlist
与另一个电话 Items
有关。
我想在每个项目的树枝模板 id 上部署。
class Playlist
{
private $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
{
$content->setPlaylist($this);
$this->duration += $content->getDuration();
$this->items->add($content);
return $this;
}
public function removeItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
{
$this->items->removeElement($content);
$this->duration -= $content->getDuration();
}
public function getItems()
{
return $this->items;
}
}
我想在 Playlist
表单中部署项目的 id。
我试过这样:
{% for content in edit_form.items %}
{{ content.getId() }}
{{ content.id() }}
{% endfor %}
但是我得到以下错误:
Method "getId" for object "Symfony\Component\Form\FormView" does not
exist Method "id" for object "Symfony\Component\Form\FormView" does
not exist
我已将 id
属性添加到我的 FormType 中:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id')
->add('position')
->add('duration')
->add('playlist')
->add('content')
;
}
但我收到以下错误:
An exception has been thrown during the rendering of a template
("Catchable Fatal Error: Object of class
Symfony\Component\Form\FormView could not be converted to string")
我也试过:
// 控制器
$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);
return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
// 树枝
{{ entity.items.id }}
但这也会引发错误:
Method "id" for object "Doctrine\ORM\PersistentCollection" does not exist
谢谢!
解决方案 1
你的解决方案的最后一部分是好的
$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);
return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
'entity' => $entity,
[...]
));
不好的是那段代码
{{ entity.items.id }}
你必须在打印 id
之前循环所有项目(从你的错误中,很清楚)
{% for item in entity.items %}
{{ item.id }}
{% endfor %}
解决方案 2(未测试)
当然,您也可以从表单的底层对象访问数据,而无需从控制器传递数据。所以你可以改变你的代码片段
来自
{% for content in edit_form.items %}
{{ content.getId() }}
{{ content.id() }}
{% endfor %}
到
{% for content in edit_form.vars.data %}
{{ content.id() }}
{% endfor %}
您的错误是您试图访问 FormView
(从控制器传递)而不是实体 "binded" 到表单
我有一个实体 Playlist
与另一个电话 Items
有关。
我想在每个项目的树枝模板 id 上部署。
class Playlist
{
private $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
{
$content->setPlaylist($this);
$this->duration += $content->getDuration();
$this->items->add($content);
return $this;
}
public function removeItem(\Publicartel\AppBundle\Entity\PlaylistContent $content)
{
$this->items->removeElement($content);
$this->duration -= $content->getDuration();
}
public function getItems()
{
return $this->items;
}
}
我想在 Playlist
表单中部署项目的 id。
我试过这样:
{% for content in edit_form.items %}
{{ content.getId() }}
{{ content.id() }}
{% endfor %}
但是我得到以下错误:
Method "getId" for object "Symfony\Component\Form\FormView" does not exist Method "id" for object "Symfony\Component\Form\FormView" does not exist
我已将 id
属性添加到我的 FormType 中:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id')
->add('position')
->add('duration')
->add('playlist')
->add('content')
;
}
但我收到以下错误:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\FormView could not be converted to string")
我也试过:
// 控制器
$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);
return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
// 树枝
{{ entity.items.id }}
但这也会引发错误:
Method "id" for object "Doctrine\ORM\PersistentCollection" does not exist
谢谢!
解决方案 1
你的解决方案的最后一部分是好的
$entity = $em->getRepository('PublicartelAppBundle:Playlist')->find($id);
return $this->render('PublicartelAppBundle:Playlist:edit.html.twig', array(
'entity' => $entity,
[...]
));
不好的是那段代码
{{ entity.items.id }}
你必须在打印 id
之前循环所有项目(从你的错误中,很清楚)
{% for item in entity.items %}
{{ item.id }}
{% endfor %}
解决方案 2(未测试)
当然,您也可以从表单的底层对象访问数据,而无需从控制器传递数据。所以你可以改变你的代码片段
来自
{% for content in edit_form.items %}
{{ content.getId() }}
{{ content.id() }}
{% endfor %}
到
{% for content in edit_form.vars.data %}
{{ content.id() }}
{% endfor %}
您的错误是您试图访问 FormView
(从控制器传递)而不是实体 "binded" 到表单