Bonjour,
Je rencontre un problème lors de la soumission de mon formulaire.
J'ai une relation ManyToOne entre Personne et Affinité. Une personne est soit un ami soit de la famille. Mais il peut y avoir plusieurs personnes qui sont amis, plusieurs qui soient de la famille. MAIS lors de la soumission de mon formulaire (j'ai un champ de type EntityType qui récupère tous mes objets Affinité), Doctrine crée une nouvele ligne à chaque fois et me met l'Id affinité de la dernière ligne créée dans la dernière ligne personne créée. Or, je souhaite qu'il crée ma nouvelle personne avec l'Id qui existe déjà en base, soit l'Id Ami si j'ai choisi ami, soit l'Id Famille si j'ai choisi famille.
Je me suis rendu compte en fait que mon champ entity type instancie un objet Affinité mais dans une variable de ma classe Affinité...
Dans ma classe, j'ai une variable 'level' et la soumission du formulaire set un objet dans cette variable, qui fait que je peux accéder d'ailleurs à cet objet de cette façon :
$affinity = $em->getRepository('MFDirectoryBundle:Affinity')->findOneBy(array('level' => $person->getAffinity()->getLevel()->getLevel()));
Voici mes deux entités Person et Affinité :
<?php
namespace MF\DirectoryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Affinity
*
* @ORM\Table(name="affinity")
* @ORM\Entity(repositoryClass="MF\DirectoryBundle\Repository\AffinityRepository")
*/
class Affinity
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="level", type="string", length=128)
*/
private $level;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set level
*
* @param string $level
* @return Affinity
*/
public function setLevel($level)
{
$this->level = $level;
return $this;
}
/**
* Get level
*
* @return string
*/
public function getLevel()
{
return $this->level;
}
/**
* toString
* @return string
*/
public function __toString()
{
return (string) $this->level;
}
}
<?php
namespace MF\DirectoryBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Person
*
* @ORM\Table(name="person")
* @ORM\Entity(repositoryClass="MF\DirectoryBundle\Repository\PersonRepository")
*/
class Person
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=128)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=128)
*/
private $lastname;
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="date", nullable=true)
*/
private $birthday;
/**
* @ORM\ManyToOne(targetEntity="MF\DirectoryBundle\Entity\Address", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $address;
/**
* @ORM\ManyToOne(targetEntity="MF\DirectoryBundle\Entity\Affinity", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $affinity;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
* @return Person
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return Person
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set birthday
*
* @param \DateTime $birthday
* @return Person
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set address
*
* @param \MF\DirectoryBundle\Entity\Address $address
* @return Person
*/
public function setAddress(\MF\DirectoryBundle\Entity\Address $address = null)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return \MF\DirectoryBundle\Entity\Address
*/
public function getAddress()
{
return $this->address;
}
/**
* Set affinity
*
* @param \MF\DirectoryBundle\Entity\Affinity $affinity
* @return Person
*/
public function setAffinity(\MF\DirectoryBundle\Entity\Affinity $affinity)
{
$this->affinity = $affinity;
return $this;
}
/**
* Get affinity
*
* @return \MF\DirectoryBundle\Entity\Affinity
*/
public function getAffinity()
{
return $this->affinity;
}
}
Voici mes deux formulaires :
<?php
namespace MF\DirectoryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class PersonType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('birthday', DateType::class)
->add('address', new AddressType())
->add('affinity', new AffinityType())
->add('save',SubmitType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MF\DirectoryBundle\Entity\Person'
));
}
public function getName()
{
return 'mf_directorybundle_person';
}
}
<?php
namespace MF\DirectoryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class AffinityType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('level', EntityType::class, array(
'class' => 'MFDirectoryBundle:Affinity'
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MF\DirectoryBundle\Entity\Affinity'
));
}
public function getName()
{
return 'mf_directorybundle_affinity';
}
}
Et voici mon controlleur :
<?php
namespace MF\DirectoryBundle\Controller;
use MF\DirectoryBundle\Entity\Person;
use MF\DirectoryBundle\Entity\Address;
use MF\DirectoryBundle\Entity\Affinity;
use MF\DirectoryBundle\Form\AffinityType;
use MF\DirectoryBundle\Form\PersonType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DirectoryController extends Controller
{
public function indexAction(Request $request)
{
$person = new Person();
$form = $this->createForm(new PersonType(), $person);
$form->handleRequest($request);
$aff=$person->getAffinity();
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Personne bien enregistrée.');
}
return $this->render('MFDirectoryBundle::index.html.twig', array(
'form' => $form->createView(),
));
}
}
Actuellement, mon objet Person c'est ça :
Object(MF\DirectoryBundle\Entity\Person)#381 (6) { ["id":"MF\DirectoryBundle\Entity\Person":private]=> NULL ["firstname":"MF\DirectoryBundle\Entity\Person":private]=> string(4) "test" ["lastname":"MF\DirectoryBundle\Entity\Person":private]=> string(4) "test" ["birthday":"MF\DirectoryBundle\Entity\Person":private]=> object(DateTime)#473 (3) { ["date"]=> string(19) "2011-01-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" } ["address":"MF\DirectoryBundle\Entity\Person":private]=> object(MF\DirectoryBundle\Entity\Address)#590 (4) { ["id":"MF\DirectoryBundle\Entity\Address":private]=> NULL ["street":"MF\DirectoryBundle\Entity\Address":private]=> string(4) "test" ["postcode":"MF\DirectoryBundle\Entity\Address":private]=> string(5) "45000" ["city":"MF\DirectoryBundle\Entity\Address":private]=> string(7) "ORLEANS" } ["affinity":"MF\DirectoryBundle\Entity\Person":private]=> object(MF\DirectoryBundle\Entity\Affinity)#484 (2) { ["id":"MF\DirectoryBundle\Entity\Affinity":private]=> NULL ["level":"MF\DirectoryBundle\Entity\Affinity":private]=> object(MF\DirectoryBundle\Entity\Affinity)#680 (2) { ["id":"MF\DirectoryBundle\Entity\Affinity":private]=> int(1) ["level":"MF\DirectoryBundle\Entity\Affinity":private]=> string(7) "Famille" } } }
Je souhaite avoir ça :
Object(MF\DirectoryBundle\Entity\Person)#381 (6) { ["id":"MF\DirectoryBundle\Entity\Person":private]=> NULL ["firstname":"MF\DirectoryBundle\Entity\Person":private]=> string(4) "test" ["lastname":"MF\DirectoryBundle\Entity\Person":private]=> string(4) "test" ["birthday":"MF\DirectoryBundle\Entity\Person":private]=> object(DateTime)#473 (3) { ["date"]=> string(19) "2011-01-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" } ["address":"MF\DirectoryBundle\Entity\Person":private]=> object(MF\DirectoryBundle\Entity\Address)#590 (4) { ["id":"MF\DirectoryBundle\Entity\Address":private]=> NULL ["street":"MF\DirectoryBundle\Entity\Address":private]=> string(4) "test" ["postcode":"MF\DirectoryBundle\Entity\Address":private]=> string(5) "45000" ["city":"MF\DirectoryBundle\Entity\Address":private]=> string(7) "ORLEANS" } ["affinity":"MF\DirectoryBundle\Entity\Person":private]=> object(MF\DirectoryBundle\Entity\Affinity)#680 (2) { ["id":"MF\DirectoryBundle\Entity\Affinity":private]=> int(1) ["level":"MF\DirectoryBundle\Entity\Affinity":private]=> string(7) "Famille" } } }
Un grand merci à vous.
Grégory