0
votes

I have an entity "produit" with this relation:

/**
 * @var \stdClass
 *
 * @ORM\ManyToMany(targetEntity="ListeBundle\Entity\GarDegatEaux", cascade={"persist"})
 * @ORM\JoinTable(name="produit_garDegatEaux",
 *      joinColumns={@ORM\JoinColumn(name="produit_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="garDegatEaux_id", referencedColumnName="id", unique=false)})
 */
private $garDegatEaux;

An a form type like below :

->add('garDegatEaux', CollectionType::class, array('entry_type' => GarDegatEauxType::class,
                                                           'allow_add' => true,
                                                           'allow_delete' => true,
                                                           //'by_reference' => false,
                                                           'prototype' => true,
                                                           'label' => 'Coefficients dégât des eaux',
                                                           'entry_options' => array('label' => 'Coefficients'),
                                                           'attr' => array('class' => 'collection')
                                                        ))

When by reference is set to false I have this error :

Neither the property "garDegatEaux" nor one of the methods "addGarDegatEau()"/"removeGarDegatEau()", "setGarDegatEaux()", "garDegatEaux()", "__set()" or "__call()" exist and have public access in class "DevisBundle\Entity\Produit".

Of course in my entity addGarDegatEau()"/"removeGarDegatEau()" and get.. exist and have public acces. I have also my construct function with:

$this->garDegatEaux = new \Doctrine\Common\Collections\ArrayCollection();

When "by_reference" is set to true there is no error but nothing is submitted for the collection. And when i dump the form before the persist there is nothing in the ArrayCollection.

And when "by_reference" is commented there is no error but nothing is persisted for the ArrayCollection too.

My controller:

public function creerProduitAction(Request $request) {

    $produit = new Produit;
    $formProduit = $this->createForm(ProduitType::class, $produit);

    $formProduit->handleRequest($request);

    if ($formProduit->isValid() && $formProduit->isSubmitted()) {



        $em = $this->getDoctrine()->getManager();

        $em->persist($produit);
        $em->flush();
    }

    return $this->render('AdminBundle:Produit:creerProduit.html.twig', array(
        'formProduit' => $formProduit->createView()
    ));
}

I used collection with symfony 2.7 and this process worked. I use symfony 2.8 atm. I don't understand why collections are not persisted.

1

1 Answers

0
votes

First of all, as Symfony's Docs say:

Similarly, if you're using the CollectionType field where your underlying collection data is an object (like with Doctrine's ArrayCollection), then by_reference must be set to false if you need the adder and remover (e.g. addAuthor() and removeAuthor()) to be called.

This is exactly your situation, therefore by_reference should be set to false.

And as for the error you get, I'm pretty sure that you don't have required getters/setters although you think you have. Please check for any typos in their names etc.