我想在奏鸣曲管理员的节目动作中展示一对多关系实体。我在(“Sonata admin bundle, manipulate objects”)找到了我的问题的答案。我尝试实现@M Khalid Junaid的解决方案,但是我收到错误“在SonataAdminBundle中,在呈现模板期间抛出异常(”警告:nl2br()期望参数1为字符串,对象给定“): CRUD:第13行的base_show_field.html.twig。“
之前有没有人遇到过这个问题?
GroupParticipant.php
class GroupRepresentive {
...
/**
* @ORM\OneToMany(targetEntity="GroupParticipant", mappedBy="representive", cascade={"persist", "remove"}, orphanRemoval=true)
*/
public $participant;
public function __construct() {
$this->participant = new ArrayCollection();
}
...}
GroupRepresentativeAdmin.php
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('eventTitle')
->add('email')
->add('person')
->add('payment.paymentType')
->add('payment.bank')
->add('payment.userAccountNumber')
->add('payment.referenceNumber')
->add('payment.paymentAt')
->end()
->with('Participant')
->add('participant', 'null', array(
'template' => 'AppBundle::Admin/groupParticipant.html.twig'
))
;
}
groupParticipant.html.twig
{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
{% block field %}
{% spaceless %}
{% endspaceless %}
{% endblock %}
我把自定义模板放在
答案 0 :(得分:4)
因为你没有真正扩展
SonataAdminBundle:CRUD:base_show_field.html.twig
试试这个
{% block field %}
{# show a field of your entity for example the name #}
{{value.name}}
{% endblock %}
答案 1 :(得分:0)
尽管先前的答案已被接受,但我为使用Symfony 4和Symfony 3.4(Symfony Flex)的用户提供了解决方案。
Admin类中的字段必须类似于:
GroupRepresentativeAdmin.php
->add('participant', 'null', array(
'template' => 'folderName/fileName.html.twig'
));
请注意,该文件夹必须位于您的模板目录中。树枝文件的路径必须为 templates / folderName / fileName.html.twig
树枝文件中的内容必须类似于:
fileName.html.twig
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}
{% block field %}
{% spaceless %}
//Your custom operation
{% endspaceless %}
{% endblock %}