Update Entity avec relation ManyToOne non fonctionnel
Bonjour à tous,
cela fait plusieurs semaines que je travail sur un projet sous symfony2.
Toutefois depuis quelques jours, je rencontre quelques problèmes lors de la validation d'un formulaire de mise à jour.
Pour situer le problème :
- Je dispose de plusieurs projets.
- Un projet est affecté à une seule activité (choix par liste déroulante).
- Une activité peut être affectée à plusieurs projets.
- A Un projet participe 1 ou plusieurs partenaires (choix par liste à choix multiples)
- Un partenaire peut participer à plusieurs projets.
Lorsque je créé un nouveau projet, toutes les informations saisies dans le formulaires sont bel et bien validées et stockées en base.
Lorsque je suis sur la page d'édition d'un projet, le formulaire récupère sans problème l'ensemble des informations stocké en base.
- Si je modifie l'activité et que je valide le formulaire, je reviens à la page d'affichage avec le message "Le projet a bien été mise à jour". Toutefois l'activité n'a pas été mise à jour.
- Si je modifie n'importe quel autre champs du formulaire. la mise à jour fonctionne aussi et mes nouvelles données sont enregistrés en base (sauf l'activité ! qui reste identique a ce qu'elle a été lors de la création)
Cela n'a pas été toujours le cas.. Mais je ne vois hélas pas où sont mes erreurs.
Vous trouverez ci-dessous les entités Projet et Activity ainsi que le ProjetController, ProjetType et ProjetHandler.
Si quelqu'un peut m'aider à me dépatouiller de ce problème bloquant.
Merci par avance :)
Entité Projet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
<?php
namespace Khyor\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Khyor\AdminBundle\Entity\Projet
*
* @ORM\Table(name="projet")
* @ORM\Entity(repositoryClass="Khyor\AdminBundle\Entity\ProjetRepository")
* @UniqueEntity(fields="code", message="projet.unique")
*/
class Projet
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="title", type="string", length=100)
* @Assert\NotBlank(message="projet.title.blank")
* @Assert\MaxLength(limit="100",message="projet.title.long")
*/
private $title;
/**
* @var string $location
*
* @ORM\Column(name="location", type="string", length=45)
* @Assert\NotBlank(message="projet.location.blank")
* @Assert\MaxLength(limit="45", message="projet.location.long")
*/
private $location;
/**
* @var text $description
*
* @ORM\Column(name="description", type="text")
* @Assert\NotBlank(message="projet.description.blank")
*/
private $description;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
/**
* @ORM\ManyToMany(targetEntity="Partner", inversedBy="projets")
* @ORM\JoinTable(name="projet_partners")
* @Assert\Valid()
*/
protected $partners;
/**
* @ORM\ManyToOne(targetEntity="Activity", inversedBy="projets", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="activity_id", projetdColumnName="id", nullable=false)
* })
* @Assert\Valid()
*/
protected $activity;
/**
* @var string $slug
*
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
public function __construct() {
$this->partners = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set location
*
* @param string $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* Get location
*
* @return string
*/
public function getLocation()
{
return $this->location;
}
/**
* Set description
*
* @param text $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return text
*/
public function getDescription()
{
return $this->description;
}
/**
* Get created
*
* @return date
*/
public function getCreated()
{
return $this->created;
}
/**
* Get updated
*
* @return date
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set activity
*
* @param Khyor\AdminBundle\Entity\Activity $activity
*/
public function setActivity(Activity $activity)
{
$this->activity = $activity;
}
/**
* Get activity
*
* @return Khyor\AdminBundle\Entity\Activity
*/
public function getActivity()
{
return $this->activity;
}
/**
* Add partner
*
*/
public function addPartner(Partner $partner)
{
$this->partners[] = $partner;
}
/**
* Get partners
*
*/
public function getPartners()
{
return $this->partners;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
} |
Entité Activité :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<?php
namespace Khyor\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Khyor\AdminBundle\Entity\Activity
*
* @ORM\Table(name="activity")
* @ORM\Entity(repositoryClass="Khyor\AdminBundle\Entity\ActivityRepository")
* @UniqueEntity(fields="name", message="activity.unique")
*/
class Activity
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
* @Assert\NotBlank(message="activity.name.blank")
* @Assert\Maxlength(limit="100",message="activity.name.long")
*/
private $name;
/**
*
* @ORM\OneToMany(targetEntity="Projet", mappedBy="activity")
* @Assert\valid()
*/
protected $projets;
public function __construct() {
$this->projets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add projets
*
* @param Khyor\AdminBundle\Entity\Projet $projets
*/
public function addProjet(\Khyor\AdminBundle\Entity\Projet $projets)
{
$this->projets[] = $projets;
}
} |
Projet Controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
<?php
namespace Khyor\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Khyor\AdminBundle\Entity\Projet;
use Khyor\AdminBundle\Form\ProjetType;
use Khyor\AdminBundle\Form\ProjetHandler;
/**
* Projet controller.
*
*/
class ProjetController extends Controller
{
...
/**
* Displays a form to edit an existing Projet entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('KhyorAdminBundle:Projet')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Projet entity.');
}
$editForm = $this->createForm(new ProjetType(), $entity);
$formHandler = new ProjetHandler($editForm, $this->get('request'), $em);
$deleteForm = $this->createDeleteForm($id);
if($formHandler->process())
{
return $this->redirect( $this->generateUrl('projet_show', array('id' => $entity->getId())) );
}
return $this->render('KhyorAdminBundle:Projet:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing Projet entity.
*
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('KhyorAdminBundle:Projet')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Projet entity.');
}
$editForm = $this->createForm(new ProjetType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$formHandler = new ProjetHandler($editForm, $this->get('request'), $em);
if( $formHandler->process() )
{
$this->get('session')->setFlash('message', 'La référence a bien été modifiée.');
return $this->redirect($this->generateUrl('projet_show', array('id' => $id)));
}
return $this->render('KhyorAdminBundle:Projet:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
...
}
{ |
Projet Handler :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
<?php
namespace Khyor\AdminBundle\Form;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
use Khyor\AdminBundle\Entity\Projet;
class ReferenceHandler
{
protected $form;
protected $request;
protected $em;
public function __construct(Form $form, Request $request, EntityManager $em)
{
$this->form = $form;
$this->request = $request;
$this->em = $em;
}
public function process()
{
if( $this->request->getMethod() == 'POST' )
{
$this->form->bindRequest($this->request);
if( $this->form->isValid() )
{
$this->onSuccess($this->form->getData());
return true;
}
}
return false;
}
public function onSuccess(Reference $projet)
{
$this->em->persist($projet);
foreach($projet->getPartners() as $partner)
{
$this->em->persist($partner);
}
}
} |
Projet Type :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
<?php
namespace Khyor\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ProjetType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title', 'text', array('label' => 'Titre'))
->add('activity', 'entity', array(
'label' => 'Activité',
'class' => 'Khyor\AdminBundle\Entity\Activity',
'property'=>'name',
'required' => true))
->add('location', 'text', array('label' => "Lieu"))
->add('partners', 'entity', array(
'label' => 'Partenaire(s)',
'class' => 'Khyor\AdminBundle\Entity\Partner',
'property' => 'name',
'required' => true,
'multiple' => true))
->add('description', 'textarea', array('label' => "Description"))
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Khyor\AdminBundle\Entity\Projet',
);
}
public function getName()
{
return 'servicad_adminbundle_projettype';
}
} |
ReferenceHandler ou ProjetHandler
Bonjour,
Vous montrez ReferenceHandler à la place de ProjetHandler, ces 2 fichiers sont similaires ?
Avez-vous une trace de vos modifs récentes via git ou svn ?