简体   繁体   中英

Sonata Admin Bundle Type Collection Customisation

For example I have 3 entities:

  • Category
  • Subcategory
  • Product

In SonataAdminBundle I'd like to be able to add Subcategory while editing Category and Products while editing Subcategory.

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?

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: 在此输入图像描述

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.

Somehow, hasParent() always return 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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