Sonata Admin Bundle 类型集合自定义 [英] Sonata Admin Bundle Type Collection Customisation
问题描述
例如我有 3 个实体:
For example I have 3 entities:
- 类别
- 子类别
- 产品
在 SonataAdminBundle 中,我希望能够在编辑类别和产品的同时添加子类别,同时编辑子类别.
In SonataAdminBundle I'd like to be able to add Subcategory while editing Category and Products while editing Subcategory.
根据这个想法,我创建了字段,但 SonataAdminBundle 开始与它们一起播放Inception".
Following this idea I created fields, but SonataAdminBundle starts playing "Inception" with them.
当我打开类别时,我会看到包含相关产品的相关子类别.
When I open Category I see related Subcategories which contain related Products.
在这种情况下,我如何切断产品"字段?
How can I cut off "Products" field in this case?
更新:
我的课程(简化版)如下所示:
My classes (simplified) look like this:
// .../CoreBundle/Admin/CategoryAdmin.php
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('name', null, array('required' => true))
->add('url', null, array('required' => true))
->add('subcategories', 'sonata_type_collection', array('by_reference' => true), array(
'edit' => 'inline',
'sortable' => 'pos',
'inline' => 'table',));
}
// .../CoreBundle/Admin/SubcategoriesAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', null, array('label' => 'name'))
->add('category_id', null, array('label' => 'Category'))
->add('url', null, array('label' => 'Url'))
->add('products', 'sonata_type_collection',
array('by_reference' => false),
array(
'edit' => 'inline',
'sortable' => 'pos',
'inline' => 'table',
));
}
// .../CoreBundle/Admin/ProductsAdmin.php
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('name', null, array('label' => 'Заголовок'))
->add('subcategory_id', null, array('label' => 'Subcategory'));
}
架构如下所示:在 AdminBundle 中,它看起来像这样:
Schema looks like this: And in AdminBundle it looks like this:
推荐答案
你为什么不尝试以下方法:
Why don't you try something along these lines:
// .../CoreBundle/Admin/SubcategoriesAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', null, array('label' => 'name'))
->add('category_id', null, array('label' => 'Category'))
->add('url', null, array('label' => 'Url'));
// only show the child form if this is not itself a child form
if (!$formMapper->getFormBuilder()->getForm()->hasParent()) {
$formmapper
->add('products', 'sonata_type_collection',
array('by_reference' => false),
array(
'edit' => 'inline',
'sortable' => 'pos',
'inline' => 'table',
));
}
}
这篇关于Sonata Admin Bundle 类型集合自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!