0

I'm having an issue in Sonata Admin Bundle (Symfony). When I want to add a child of an entity in the same edit form I expect that the add button add one input field for a child entity, but the add button is adding always the already same cuantity of child entities.

For example, i have a Establishment witch haves two Sectors listed. If I want to add a Sector in the edit form, sonata add three more sectors in the edit.

Is there anyone who is experienced the same? Did solve it?

EDIT 1

I have 3 classes with relations oneToMany:

let's say 'Establishment', 'SecEstablishment' and 'FlatSecEstablishment'. A establishmente can have cero or more SecEstablishments and a SecEstablishment can have one or more FlatSecEstablishments. Hope this code can help to understand what I need to do.

class EstablishmentControllerAdmin extends 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();

}

}

class SecEstablishmentControllerAdmin extends 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 extends AbstractAdmin {

protected $parentAssociationMapping = 'secEstablishment';

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

}

Matias Vadino
  • 127
  • 1
  • 1
  • 8

0 Answers0