[英]Multiple nested collection fields in Sonata Admin
我在使用 Sonata Admin 方面經驗不足,我需要幫助。
有 4 個實體:Poll、Field(問題)、PollHasField、Option(答案)。 我需要制作一頁 PollAdmin,以便可以為它們創建字段和選項。
現在我設法制作了一個 FieldAdmin 頁面,您可以在其中創建選項,以及一個 PollAdmin 頁面,您可以在其中添加現有字段。 但是,當我嘗試通過在 PollHasFieldAdmin 中設置“sonata_type_collection”類型來將 FieldAdmin 與 PollHasFieldAdmin 綁定時,出現錯誤:
request.CRITICAL:未捕獲 PHP 異常 Symfony\Component\Form\Exception\UnexpectedTypeException:“類型為“array or \Traversable”的預期參數”,“Proxies_ CG _\SIP\ResourceBundle\Entity\Poll\Field”在 C:\ wamp64\www\butler.backend\vendor\sonata-project\core-bundle\Form\EventListener\ResizeFormListener.php 第 96 行
輪詢管理員class :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('title', null, array('label' => 'sip.customer.title'))
->add('active', null, array('label' => 'is active'))
->add('howOftenToShow', null, array('label' => 'Frequency'))
->add('fields', 'sonata_type_collection', array(
'label' => 'Fields',
'cascade_validation' => true,
'by_reference' => false,
'required' => false,
'attr' => array(
'class' => 'form-control'
)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'admin_code' => 'sip.content.pollhasfield.admin',
)
)
->end()
;
}
PollHasFieldAdmin class:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('field', 'sonata_type_collection', array(
'label' => 'Options',
'cascade_validation' => true,
'by_reference' => false,
'required' => false,
'attr' => array(
'class' => 'form-control'
)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'admin_code' => 'sip.content.field.admin',
)
)
->add('position', 'hidden',
array(
'label' => 'sip_position',
)
)
->end();
}
現場管理員class
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('title', null, array('label' => 'sip.customer.title'))
->add('type', 'choice', array('label' => 'Type', 'choices' => Field::getTypes()))
->add('options', 'sonata_type_collection', array(
'label' => 'Options',
'cascade_validation' => true,
'by_reference' => false,
'required' => false,
'attr' => array(
'class' => 'form-control'
)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'admin_code' => 'sip.content.option.admin',
)
)
->end()
;
}
選項管理員 class :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('title', null, array('label' => 'sip.customer.title'))
->add('position', null, array('label' => 'sip_position'))
->end()
;
}
我究竟做錯了什么?
修復了 Poll class 中 OneToMany 的替換 ManyToMany 關系。因此不再需要 PollHasField\PollHasFieldAdmin 類。
輪詢管理員class :
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('title', null, array('label' => 'sip.customer.title'))
->add('active', null, array('label' => 'Active'))
->add('howOftenToShow', null, array('label' => 'Frequency'))
->add('fields', 'sonata_type_collection', array(
'label' => 'Fields',
'cascade_validation' => true,
'by_reference' => false,
'required' => false,
'attr' => array(
'class' => 'form-control'
)
), array(
'edit' => 'inline',
'inline' => 'table',
'admin_code' => 'sip.content.field.admin',
)
)
->end()
;
}
此外,因為項目使用 SonataAdminBundle 2.4,所以我必須從這個拉取請求https://github.com/sonata-project/SonataAdminBundle/pull/3553添加對 Sonata\AdminBundle\Admin\AdminHelper class 中嵌套(> 2 級)sonata_type_collection 的支持
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.