简体   繁体   English

Sonata Admin捆绑类型集合定制

[英]Sonata Admin Bundle Type Collection Customisation

For example I have 3 entities: 例如,我有3个实体:

  • Category 类别
  • Subcategory 子目录
  • Product 产品

In SonataAdminBundle I'd like to be able to add Subcategory while editing Category and Products while editing Subcategory. 在SonataAdminBundle中,我希望能够在编辑子类别时编辑类别和产品时添加子类别。

Following this idea I created fields, but SonataAdminBundle starts playing "Inception" with them. 按照这个想法,我创建了字段,但SonataAdminBundle开始与它们一起玩“Inception”。

When I open Category I see related Subcategories which contain related Products. 当我打开类别I时,查看包含相关产品的相关子类别。

How can I cut off "Products" field in this case? 在这种情况下,如何切断“产品”字段?

Update: 更新:

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'));
}

Schema looks like this: 架构看起来像这样: 在此输入图像描述 And in AdminBundle it looks like this: 在AdminBundle中它看起来像这样: 在此输入图像描述

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',
                  ));
    }
}

The solution given by @likeitlikeit does not work for symfony2.0. @likeitlikeit给出的解决方案对symfony2.0不起作用。

Somehow, hasParent() always return false. 不知何故,hasParent()总是返回false。

As a workaround : 作为解决方法:

if (!is_numeric($formMapper->getFormBuilder()->getForm()->getName())) {}

The name in a collection will be numeric (0, 1, 2,...) while in a solo form it will be a hash. 集合中的名称将是数字(0,1,2,...),而在单独形式中它将是一个哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM