Sonata Admin Bundle Type Collection Customisation

拥有回忆 提交于 2019-12-03 07:21:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!