Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 1:M / 1:1 Relationship and Sonata Admin Form
    primarykey
    data
    text
    <p>I've hitting my head against the wall for countless hours now and I hope SO can be of help!</p> <p>I have Retailer, Branch and RetailerBranches entities which work just fine, retailers can have many branches and a branch can only have one retailer. The hard part happens when trying to make Sonata Admin (SonataAdminBundle) play nice with that relationship. In their simplest form, they look like this:</p> <p><strong>Retailer entity</strong></p> <pre><code> /** * @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-&gt;branches = new ArrayCollection(); } </code></pre> <p><strong>RetailerBranches join table</strong></p> <pre><code> /** * @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; </code></pre> <p><strong>Branch entity</strong></p> <pre><code> /** * @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; </code></pre> <p>The harder part happens when trying generate the form to allow that relationship to take shape:</p> <p><strong>RetailerAdmin</strong></p> <pre><code>protected function configureFormFields(FormMapper $formMapper) { $formMapper -&gt;with('Branches') -&gt;add('branches', 'sonata_type_collection', array( 'required' =&gt; false, 'by_reference' =&gt; false ), array( 'edit' =&gt; 'inline', 'inline' =&gt; 'table', )) -&gt;end() ; } </code></pre> <p><strong>RetailerBranchesAdmin</strong></p> <pre><code>protected function configureFormFields(FormMapper $formMapper) { if ($this-&gt;hasRequest()) { $link_parameters = array('context' =&gt; $this-&gt;getRequest()-&gt;get('context')); } else { $link_parameters = array(); } $formMapper -&gt;add('succursale', 'sonata_type_model_list', array( 'class' =&gt; 'VeloRetailerBundle:Branch', 'required' =&gt; false, ), array( 'edit' =&gt; 'inline', 'inline' =&gt; 'table', )) ; } </code></pre> <p><strong><em>The problem:</em></strong></p> <p>All this sort of works, here's a screenshot: <img src="https://i.stack.imgur.com/YPX0L.png" alt="enter image description here"></p> <p>There's a Retailer and its Branches. Yay.</p> <p>Problem 1: The "Add new" button at the bottom attempts to add a RetailerBranches object instead of a simple Branch object which obviously doesn't work.</p> <p>Problem 2: This method also doesn't allow the user to modify a Branch inline.</p> <p>I feel like I'm close to the solution, but I just cannot quite get there. Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload