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
private $id; 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: 
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!