Problème relation ManyToMany et inverse side
Bonjour,
J'ai une Entity CodeRome qui a un lien manytomany avec une entity SousDomaine.
J'essaie de faire un formulaire afin de ne modifier que ce champ de l'entity CodeRome. Tout semble bien se passer: à la validation du formulaire, la redirection a bien lieu. Mais les associations ne sont pas créées en base, et au final, le champ sousdomaines de CodeRome n'a pas été mis à jour.
Comment doit-on procéder pour réaliser ce genre de tâches ?
Merci d'avance
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
|
// CodeRomeController.php
/**
* @Route("/rome/{slug}/domaine/add", name="_rome_add_domaine")
* @Template
*/
public function addDomaineAction(Request $request, $slug)
{
$codeRome = $this->getCodeRome($slug);
$form = $this->createForm( new RomeSsDomaineType(), $codeRome);
if ( $request->getMethod() == 'POST' )
{
$form->bindRequest($request);
if ( $form->isValid() )
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($codeRome);
$em->flush();
$this->get('session')->setFlash('success', 'Un sous-domaine a été ajouté au code ROME '.$codeRome->getCodeV3());
return $this->redirect( $this->generateUrl('_rome_show', array('slug'=>$slug)) );
}
}
return array('form'=>$form->createView(), 'codeRome'=>$codeRome);
}
/**
* Find a CodeRome by its slug
* @param string $slug
* @return \AcDijon\GretabotBundle\Entity\CodeRome
*/
protected function getCodeRome($slug)
{
$codeRome = $this->getDoctrine()
->getRepository('AcDijonGretabotBundle:CodeRome')
->findOneBySlug($slug);
if ( !$codeRome ) throw $this->createNotFoundException('CodeRome not found');
return $codeRome;
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| // RomeSsDomaineType.php
class RomeSsDomaineType extends AbstractType
{
public function buildForm( FormBuilder $builder, array $options )
{
$builder->add('sousdomaines');
}
public function getName()
{
return 'romessdomaine';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'AcDijon\GretabotBundle\Entity\CodeRome'
);
}
} |
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
| // CodeRome.php
/**
* @ORM\Entity(repositoryClass="AcDijon\GretabotBundle\Repository\CodeRomeRepository")
* @ORM\Table(name="gbot_CodeRome")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class CodeRome {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
*
* @var string codeV2
* @ORM\Column(length=5)
* @Assert\NotBlank(message="Veuillez indiquer le code V2")
*/
protected $codeV2;
/**
*
* @var string nomV2
* @ORM\Column(length=255)
* @Assert\NotBlank(message="Veuillez renseigner le nom V2")
*/
protected $nomV2;
/**
*
* @var string appellationV2
* @ORM\Column(length=255)
* @Assert\NotBlank(message="Veuillez renseigner l'appellation V2")
*/
protected $appellationV2;
/**
*
* @var string codeV3
* @ORM\Column(length=5)
* @Assert\NotBlank(message="Veuillez indiquer le code V3")
*/
protected $codeV3;
/**
*
* @var string nomV3
* @ORM\Column(length=255)
* @Assert\NotBlank(message="Veuillez renseigner le nom V3")
*/
protected $nomV3;
/**
*
* @var string appellationV3
* @ORM\Column(length=255)
* @Assert\NotBlank(message="Veuillez renseigner l'appellation V3")
*/
protected $appellationV3;
/**
*
* @var ArrayCollection offres
* @ORM\ManyToMany(targetEntity="Offre", mappedBy="codesRome")
*/
protected $offres;
/**
*
* @var ArrayCollection codesNsf
* @ORM\ManyToMany(targetEntity="CodeNsf", inversedBy="codesRome")
* @ORM\JoinTable(name="gbot_codesRome_codesNsf")
*/
protected $codesNsf;
/**
*
* @var ArrayCollection formacodes
* @ORM\ManyToMany(targetEntity="Formacode", inversedBy="codesRome")
* @ORM\JoinTable(name="gbot_codesRome_formacodes")
*/
protected $formacodes;
/**
*
* @var ArrayCollection metiers
* @ORM\OneToMany(targetEntity="Metier", mappedBy="codeRome")
*/
protected $metiers;
/**
* @var ArrayCollection sousdomaines
* @ORM\ManyToMany(targetEntity="SousDomaine", mappedBy="codesRome")
*/
protected $sousdomaines;
/**
* @Gedmo\Slug(fields={"nomV3"})
* @ORM\Column(length=128, unique=true)
*/
protected $slug;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
protected $updated;
/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
protected $deletedAt;
public function __construct()
{
$this->offres = new \Doctrine\Common\Collections\ArrayCollection();
$this->codesNsf = new \Doctrine\Common\Collections\ArrayCollection();
$this->formacodes = new \Doctrine\Common\Collections\ArrayCollection();
$this->metiers = new \Doctrine\Common\Collections\ArrayCollection();
$this->sousdomaines = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->getCodeV3().' : '.$this->getAppellationV3();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set codeV2
*
* @param string $codeV2
*/
public function setCodeV2($codeV2)
{
$this->codeV2 = $codeV2;
}
/**
* Get codeV2
*
* @return string
*/
public function getCodeV2()
{
return $this->codeV2;
}
/**
* Set nomV2
*
* @param string $nomV2
*/
public function setNomV2($nomV2)
{
$this->nomV2 = $nomV2;
}
/**
* Get nomV2
*
* @return string
*/
public function getNomV2()
{
return $this->nomV2;
}
/**
* Set appellationV2
*
* @param string $appellationV2
*/
public function setAppellationV2($appellationV2)
{
$this->appellationV2 = $appellationV2;
}
/**
* Get appellationV2
*
* @return string
*/
public function getAppellationV2()
{
return $this->appellationV2;
}
/**
* Set codeV3
*
* @param string $codeV3
*/
public function setCodeV3($codeV3)
{
$this->codeV3 = $codeV3;
}
/**
* Get codeV3
*
* @return string
*/
public function getCodeV3()
{
return $this->codeV3;
}
/**
* Set nomV3
*
* @param string $nomV3
*/
public function setNomV3($nomV3)
{
$this->nomV3 = $nomV3;
}
/**
* Get nomV3
*
* @return string
*/
public function getNomV3()
{
return $this->nomV3;
}
/**
* Set appellationV3
*
* @param string $appellationV3
*/
public function setAppellationV3($appellationV3)
{
$this->appellationV3 = $appellationV3;
}
/**
* Get appellationV3
*
* @return string
*/
public function getAppellationV3()
{
return $this->appellationV3;
}
/**
* Set slug
*
* @param string $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set created
*
* @param datetime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set deletedAt
*
* @param datetime $deletedAt
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
}
/**
* Get deletedAt
*
* @return datetime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Add offres
*
* @param AcDijon\GretabotBundle\Entity\Offre $offres
*/
public function addOffre(\AcDijon\GretabotBundle\Entity\Offre $offres)
{
$this->offres[] = $offres;
}
/**
* Get offres
*
* @return Doctrine\Common\Collections\Collection
*/
public function getOffres()
{
return $this->offres;
}
/**
* Add codesNsf
*
* @param AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf
*/
public function addCodeNsf(\AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf)
{
$this->codesNsf[] = $codesNsf;
}
/**
* Get codesNsf
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCodesNsf()
{
return $this->codesNsf;
}
/**
* Add formacodes
*
* @param AcDijon\GretabotBundle\Entity\Formacode $formacodes
*/
public function addFormacode(\AcDijon\GretabotBundle\Entity\Formacode $formacodes)
{
$this->formacodes[] = $formacodes;
}
/**
* Get formacodes
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFormacodes()
{
return $this->formacodes;
}
/**
* Add metiers
*
* @param AcDijon\GretabotBundle\Entity\Metier $metiers
*/
public function addMetier(\AcDijon\GretabotBundle\Entity\Metier $metiers)
{
$this->metiers[] = $metiers;
}
/**
* Get metiers
*
* @return Doctrine\Common\Collections\Collection
*/
public function getMetiers()
{
return $this->metiers;
}
/**
* Add sousdomaines
*
* @param AcDijon\GretabotBundle\Entity\SousDomaine $sousdomaines
*/
public function addSousDomaine(\AcDijon\GretabotBundle\Entity\SousDomaine $sousdomaines)
{
$this->sousdomaines[] = $sousdomaines;
}
/**
* Get sousdomaines
*
* @return Doctrine\Common\Collections\Collection
*/
public function getSousdomaines()
{
return $this->sousdomaines;
}
} |
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
| /**
* @ORM\Entity(repositoryClass="AcDijon\GretabotBundle\Repository\SousDomaineRepository")
* @ORM\Table(name="gbot_SousDomaine")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class SousDomaine
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @Assert\NotBlank(message="L'intitulé doit être renseigné")
*/
protected $nom;
/**
*
* @var integer numero
* @ORM\Column(type="integer", nullable=false)
* @Assert\NotBlank(message="Veuillez donner un numéro à ce sous-domaine")
* @Assert\Type(type="integer", message="La valeur {{ value }} n'est pas un {{ type }} valide")
*/
protected $numero;
/**
* @Gedmo\Slug(fields={"nom"})
* @ORM\Column(length=128, unique=true)
*/
protected $slug;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
protected $updated;
/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
protected $deletedAt;
/**
* @ORM\ManyToOne(targetEntity="Domaine", inversedBy="sousDomaines")
* @ORM\JoinColumn(name="domaine_id", referencedColumnName="id")
*/
protected $domaine;
/**
* @var ArrayCollection formacodes
* @ORM\ManyToMany(targetEntity="Formacode")
* @ORM\JoinTable(name="gbot_sousDomaines_formacodes",
* joinColumns={@ORM\JoinColumn(name="sousdomaine_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="formacode_id", referencedColumnName="id")}
* )
* @Assert\NotBlank(message="Sélectionnez un ou plusieurs Formacodes")
*/
protected $formacodes;
/**
*
* @var ArrayCollection codesRome
* @ORM\ManyToMany(targetEntity="CodeRome", inversedBy="sousdomaines")
* @ORM\JoinTable(name="gbot_sousDomaines_codesRomes")
* @Assert\NotBlank(message="Sélectionnez un ou plusieurs code ROME")
*/
protected $codesRome;
/**
*
* @var ArrayCollection codesNsf
* @ORM\ManyToMany(targetEntity="CodeNsf")
* @ORM\JoinTable(name="gbot_sousDomaines_codesNsf",
* joinColumns={@ORM\JoinColumn(name="sousdomaine_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="codensf_id", referencedColumnName="id")}
* )
* @Assert\NotBlank(message="Sélectionner un ou plusieurs code NSF")
*/
protected $codesNsf;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set domaine
*
* @param AcDijon\GretabotBundle\Entity\Domaine $domaine
*/
public function setDomaine(\AcDijon\GretabotBundle\Entity\Domaine $domaine)
{
$this->domaine = $domaine;
}
/**
* Get domaine
*
* @return AcDijon\GretabotBundle\Entity\Domaine
*/
public function getDomaine()
{
return $this->domaine;
}
public function __construct()
{
$this->formacodes = new \Doctrine\Common\Collections\ArrayCollection();
$this->codesRome = new \Doctrine\Common\Collections\ArrayCollection();
$this->codesNsf = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString() {
return $this->getNom();
}
/**
* Add formacodes
*
* @param AcDijon\GretabotBundle\Entity\Formacode $formacodes
*/
public function addFormacode(\AcDijon\GretabotBundle\Entity\Formacode $formacodes)
{
$this->formacodes[] = $formacodes;
}
/**
* Get formacodes
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFormacodes()
{
return $this->formacodes;
}
/**
* Set slug
*
* @param string $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set created
*
* @param datetime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set deletedAt
*
* @param datetime $deletedAt
*/
public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
}
/**
* Get deletedAt
*
* @return datetime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Set nom
*
* @param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Add codesRome
*
* @param AcDijon\GretabotBundle\Entity\CodeRome $codesRome
*/
public function addCodeRome(\AcDijon\GretabotBundle\Entity\CodeRome $codesRome)
{
$this->codesRome[] = $codesRome;
}
/**
* Get codesRome
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCodesRome()
{
return $this->codesRome;
}
/**
* Add codesNsf
*
* @param AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf
*/
public function addCodeNsf(\AcDijon\GretabotBundle\Entity\CodeNsf $codesNsf)
{
$this->codesNsf[] = $codesNsf;
}
/**
* Get codesNsf
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCodesNsf()
{
return $this->codesNsf;
}
/**
* Set numero
*
* @param integer $numero
*/
public function setNumero($numero)
{
$this->numero = $numero;
}
/**
* Get numero
*
* @return integer
*/
public function getNumero()
{
return $this->numero;
}
} |