Create new brand from company: embed admin in admin in SonatAdminBundle

72 views Asked by At

I have this two tables (see pics below) mapped as follow:

enter image description here

class Brand
{
    ...

    /**
     * @var Company
     *
     * @ORM\ManyToOne(targetEntity="Company")
     * @ORM\JoinColumn(name="companies_id", referencedColumnName="id")
     */
    protected $company;

}

class Company
{
    ...  
}

I have two admin classes: CompanyAdmin and BrandAdmin. I need to add support for add a new brand - create a new brand not link a existent one - from company admin but I have not idea in how to achieve this. The idea is:

  • Create a company
  • Add a button to company form where I can keep the current company ID and link that button to brand admin so I can easily add new brands to that company

I was reading sonata docs but EMBEDDING OTHER ADMINS section is not ready yet. Can any help me to get this done? I am stucked

1

There are 1 answers

0
HypeR On BEST ANSWER

I didn't fully understand your request, but from what I understood :

To create / select / delete company from brand

class BrandAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('company', 'sonata_type_model_list', array(
            'by_reference' => false
        ));
    }
}

Documentation: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#advanced-usage-many-to-one

Add new brands in Company form

class CompanyAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('brands', 'sonata_type_collection',
                array(
                    'label' => false,
                    'by_reference' => false
                ),
                array(
                    'edit' => 'inline',
                    'inline' => 'table'
                )
            );
    }
}

The fields displayed to create a new brand will be based on the configureFormFields of BrandAdmin.

Documentation : https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html#sonata-type-collection