I'm running into a problem to persist association one-to-many / many-to-one.
My goal is to have a form to save the data "structure", among these are the contacts that are rendered as a collection.
The db is as follows:
Structure
class Structure
{
/**
* @ORM\OneToMany(targetEntity="ContactStructure", mappedBy="structure_have_contact", cascade={"all"})
*/
private $contacts;
[...]
/**
* Add contacts
*
* @param \Acme\CoreBundle\Entity\ContactStructure $contacts
* @return Structure
*/
public function addContact(ContactStructure $contacts)
{
$this->contacts->add($contacts);
}
Contact
class Contact
{
/**
* @ORM\OneToMany(targetEntity="ContactStructure", mappedBy="type_contact", cascade={"all"})
*/
private $typology;
[...]
/**
* Add typology
*
* @param \Acme\CoreBundle\Entity\ContactStructure $typology
* @return Contact
*/
public function addTypology(\Acme\CoreBundle\Entity\ContactStructure $typology)
{
$this->typology[] = $typology;
return $this;
}
ContactStructure
class ContactStructure
{
/**
* @var string
*
* @ORM\Column(name="contact", type="string", length=255)
*/
private $contact;
/**
* @ORM\ManyToOne(targetEntity="Structure", inversedBy="contacts", cascade={"all"})
* @ORM\JoinColumn(name="structure_id", referencedColumnName="id")
*/
private $structure_have_contact;
/**
* @ORM\ManyToOne(targetEntity="Contact", inversedBy="typology", cascade={"all"})
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
*/
private $type_contact;
[...]
/**
* Set structure_have_contact
*
* @param \Acme\CoreBundle\Entity\Structure $structureHaveContact
* @return ContactStructure
*/
public function setStructureHaveContact(\Acme\CoreBundle\Entity\Structure $structureHaveContact = null)
{
$this->structure_have_contact = $structureHaveContact;
return $this;
}
/**
* Set type_contact
*
* @param $typeContact
* @return ContactStructure
*/
public function setTypeContact($typeContact = null)
{
$this->type_contact = $typeContact;
return $this;
}
StructureType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('contacts', 'collection', array(
'type' => new ContactStructureType(),
'label' => false,
'allow_add' => true,
'allow_delete' => true,
))
[...]
ContactStructureType
$builder
->add('type_contact', 'entity', array(
'class' => 'AcmeCoreBundle:Contact' ,
'property' => 'type'
))
->add('contact')
;
Controller
if ($form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($structure);
foreach ($structure->getContacts() as $contact) {
$contact->setTypeContact($structure);
$em->persist($contact);
}
$em->flush();
return $this->redirect($this->generateUrl('structure_show', array('id' => $structure->getId())));
}
When I save the form, I get this error:
Found entity of type Acme\CoreBundle\Entity\Structure on association Acme\CoreBundle\Entity\ContactStructure#type_contact, but expecting Acme\CoreBundle\Entity\Contact
Any idea where I'm wrong?
ContactStructure Entity,
setTypeContact() method need $typeContact is object of Contact Class