Reputation: 8548
I have an issue with one of my many-to-many relations. I have debugged my form and I see that the chosen objects are actually in the entity object after form validation, but it never gets saved to the database, so there must be an issue with my mapping code, but I copy-pasted from a working one just substituted the fields and paths...
Here's the company object's relevant code
/**
* @ORM\ManyToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Template", inversedBy="companies")
* @ORM\JoinTable(name="templatePermissions")
*/
private $templatePermissions;
/**
* Add templatePermissions
*
* @param BizTV\ContentManagementBundle\Entity\Template $templatePermissions
*/
public function addTemplate(\BizTV\ContentManagementBundle\Entity\Template $templatePermissions)
{
$this->templatePermissions[] = $templatePermissions;
}
and the template object
/**
* @ORM\ManyToMany(targetEntity="BizTV\BackendBundle\Entity\company", mappedBy="templatePermissions")
*/
private $companies;
/**
* Add companies
*
* @param BizTV\BackendBundle\Entity\company $companies
*/
public function addCompany(\BizTV\BackendBundle\Entity\company $companies)
{
$this->companies[] = $companies;
}
/**
* Get companies
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCompanies()
{
return $this->companies;
}
The code for updating (and creating is similar, and has the same problem) is just a standard ...
public function updateAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVContentManagementBundle:Template')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Template entity.');
}
$editForm = $this->createForm(new TemplateType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
//DEBUG
// foreach($entity->getCompanies() as $c) {
// echo $c->getCompanyName();
// }
// die;
$em->persist($entity);
$em->flush();
$this->getRequest()->getSession()->setFlash('success', 'Template '.$entity->getId().' har uppdaterats.' );
return $this->redirect($this->generateUrl('listTemplates'));
}
Here's my form, like I said, it works perfectly for putting the stuff into my object (template entity) but it doesn't get persisted to DB...
$builder
->add('companies', 'entity', array(
'label' => 'Företag som har tillgång till mallen',
'multiple' => true, // Multiple selection allowed
'expanded' => true, // Render as checkboxes
'property' => 'company_name',
'class' => 'BizTV\BackendBundle\Entity\company',
))
;
What am I missing?
Upvotes: 0
Views: 1016
Reputation: 11364
Pay attention for picking owning/inverse side. The owning side should be the one which is responsible for persist action. In your case owning side should be Template entity not Company.
For more info look here:
Have you company entity class name starts with lower case? If no, you have a typo:
BizTV\BackendBundle\Entity\company
try add cascade on persist
At the end your relation definition in Company entity should look sth like this:
/**
* @ORM\ManyToMany(targetEntity="BizTV\BackendBundle\Entity\company", inversedBy="templatePermissions", cascade={"persist"})
*/
private $companies;
Upvotes: 3