Sonata管理员添加多个子实体

时间:2017-07-11 18:47:38

标签: php symfony sonata-admin

我在Sonata Admin Bundle(Symfony)中遇到了问题。当我想在同一编辑表单中添加实体的子项时,我希望添加按钮为子实体添加一个输入字段,但添加按钮总是添加已经相同的子实体数。

例如,我有一个建立女巫有两个部门列出。如果我想在编辑表单中添加一个扇区,奏鸣曲会在编辑中添加三个扇区。

有没有经验相同的人?解决了吗?

编辑1

我有3个关系oneToMany的课程:

让我们说'建立','SecEstablishment'和'FlatSecEstablishment'。企业可以拥有cero或更多的SecEstablishments,而SecEstablishment可以拥有一个或多个FlatSecEstablishments。希望这段代码有助于理解我需要做什么。

类EstablishmentControllerAdmin扩展了AbstractAdmin {

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->with('Details')
                ->add('id','text', array('label' => 'Id', 'disabled' => true))
                ->add('establishment', 'text', array ('label' => 'establishment'))
                ->add('address', 'text', array('label' => 'Address', 'required' => false))
                ->add('phone', 'text', array('label' => 'Phone', 'required' => false))
                ->add('postalCode', 'integer', array('label' => 'Postal Code', 'required' => false))
           ->end()

        ->with('Establishment sectors')
            ->add('sectores','sonata_type_collection', 
                array(
                    'btn_add' => 'New Sector',
                    'type_options' => array('delete' => false)
                ), 
                array(
                    'edit' => 'inline',
                    'inline' => 'table'
                ))
        ->end();

}

}

类Sec​​EstablishmentControllerAdmin扩展AbstractAdmin {

protected $parentAssociationMapping = 'establishment';

protected function configureFormFields(FormMapper $formMapper) {

    if($this->hasParentFieldDescription()) {
    $formMapper
            ->with('Details')
                ->add('detSec', 'text')
                ->add('flatStructure', 'sonata_type_model', array(

                        'btn_list' => 'Select',
                        'btn_add' => false,
                        'btn_delete' => false,
                        'required' => false
                      ), array(
                          'placeholder' => 'Select Flat Structure',
                          'nullable' => true,
                      ))
            ->end();
    } else {
        $formMapper
            ->with('Details')
                ->add('flatStructure', 'sonata_type_model', array(

                        'btn_list' => 'Select',
                        'btn_add' => false,
                        'btn_delete' => false,
                        'required' => false
                      ), array(
                          'placeholder' => 'Select Flat Structure',
                          'nullable' => true,
                      ))
                ->add('secFlat','sonata_type_collection', 
                    array(
                         'btn_add' => false,
                         'type_options' => array('delete' => false)
                     ), 
                     array(
                         'edit' => 'inline',
                         'inline' => 'table'
                     ))
                ->end();
    }
}

}

class FlatSecEstablishmentControllerAdmin扩展AbstractAdmin {

protected $parentAssociationMapping = 'secEstablishment';

protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
        ->with('Details')
            ->add('detFlat', 'text', array('label' => 'Flat Detail'))
        ->end();
}

}

0 个答案:

没有答案