I've hitting my head against the wall for countless hours now and I hope SO can be of help!
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:
Retailer entity
/**
* @ORMColumn(name="ID", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Relation
*
* @ORMOneToMany(targetEntity="RetailerBranches", mappedBy="Retailer", cascade={"persist"})
*/
protected $branches;
public function __construct() {
$this->branches = new ArrayCollection();
}
RetailerBranches join table
/**
* @ORMColumn(name="ID", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORMJoinColumn(name="Retailer_ID", referencedColumnName="ID", nullable=false)
* @ORMManyToOne(targetEntity="Retailer", inversedBy="branches")
*/
private $retailer;
/**
* @ORMJoinColumn(name="Branch_ID", referencedColumnName="ID", nullable=false, unique=true)
* @ORMOneToOne(targetEntity="Branch", inversedBy="retailer")
*/
private $branch;
Branch entity
/**
* @ORMColumn(name="ID", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Relation
*
* @ORMOneToOne(targetEntity="RetailerBranches", mappedBy="branch", cascade={"persist"})
*/
private $retailer;
The harder part happens when trying generate the form to allow that relationship to take shape:
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',
))
;
}
The problem:
All this sort of works, here's a screenshot:
There's a Retailer and its Branches. Yay.
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.
Problem 2: This method also doesn't allow the user to modify a Branch inline.
I feel like I'm close to the solution, but I just cannot quite get there. Any help would be greatly appreciated!
See Question&Answers more detail:
os