Symfony2 1: M / 1: 1 Communication and Sonata Control Form

Now I hit my head against the wall for countless hours, and I hope SO can help!

I have Retailer, Branch, and RetailerBranches that work very well, retailers can have many branches, and a branch can only have one retailer. The hard part comes when you try to get Sonata Admin (SonataAdminBundle) to play well with this relationship. In their simplest form, they look like this:

Seller Object

/** * @ORM\Column(name="ID", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * Relation * * @ORM\OneToMany(targetEntity="RetailerBranches", mappedBy="Retailer", cascade={"persist"}) */ protected $branches; public function __construct() { $this->branches = new ArrayCollection(); } 

RetailerBranches join table

  /** * @ORM\Column(name="ID", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @ORM\JoinColumn(name="Retailer_ID", referencedColumnName="ID", nullable=false) * @ORM\ManyToOne(targetEntity="Retailer", inversedBy="branches") */ private $retailer; /** * @ORM\JoinColumn(name="Branch_ID", referencedColumnName="ID", nullable=false, unique=true) * @ORM\OneToOne(targetEntity="Branch", inversedBy="retailer") */ private $branch; 

Branch

  /** * @ORM\Column(name="ID", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * Relation * * @ORM\OneToOne(targetEntity="RetailerBranches", mappedBy="branch", cascade={"persist"}) */ private $retailer; 

The more complicated part arises when trying to generate a form so that this relation gets a form:

Retaileradmin

 protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Branches') ->add('branches', 'sonata_type_collection', array( 'required' => false, 'by_reference' => false ), array( 'edit' => 'inline', 'inline' => 'table', )) ->end() ; } 

RetailerBranchesAdmin

 protected function configureFormFields(FormMapper $formMapper) { if ($this->hasRequest()) { $link_parameters = array('context' => $this->getRequest()->get('context')); } else { $link_parameters = array(); } $formMapper ->add('succursale', 'sonata_type_model_list', array( 'class' => 'VeloRetailerBundle:Branch', 'required' => false, ), array( 'edit' => 'inline', 'inline' => 'table', )) ; } 

Problem:

All such works, here is a screenshot: enter image description here

There is a retailer and its branches. Yay

Problem 1: The Add New button at the bottom is trying to add a RetailerBranches object instead of a simple Branch object that is clearly not working.

Problem 2: This method also prevents the user from changing the inline branch.

I feel like I'm close to a solution, but I just can't get there. Any help would be greatly appreciated!

+3
source share
2 answers

For those working with the same issue, I posted the solution on GitHub

.

+4
source

When you need to edit OneToOne or other relationships on the same page in Sonata Admin, you can also create Admin classes for each Entity (and add to config.yml) and just add the entire object to your form in your main Admin class, for example like this:

 protected function configureFormFields(FormMapper $formMapper) $formMapper ->add('yourLinkedProperty', 'sonata_type_admin') //other form fields ->end() 

see Sonata Doc http://sonata-project.org/bundles/admin/master/doc/reference/form_types.html

+1
source

Source: https://habr.com/ru/post/1437390/


All Articles