Bonsoir à tous,
Pour mon premier post, je commence fort !
Je n'arrive pas à faire prendre en compte une modification pour une relation ManyToMany entre deux entités (Brand et User), mais uniquement dans un sens.
Explications:
J'ai un formulaire pour modifier un utilisateur, je peux lui attribuer 0 ou plusieurs marques (input type="box") et ça marche !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?php //Class User /** * @ORM\ManyToMany(targetEntity="Brand", inversedBy="users", cascade={"persist"}) * @Assert\Valid() */ private $brands; // Class Brand /** * @ORM\ManyToMany(targetEntity="User", mappedBy="brands", cascade={"persist"}) * @Assert\Valid() */ private $users;
J'ai un formulaire pour modifier une marque, je peux lui attribuer 0 ou plusieurs utilisateurs (input type="box") et ça ne marche pas !
Les modifications sont bien prises en compte sauf que l'utilisateur n'est pas lié à la marque. Alors que l'inverse marche !!
Voici mon handler lors du clique sur modifier dans la partie modification de la marque:
La seule partie qui ne marche pas est la dernière boucle foreach() (qui est sur les utilisateurs à lier à la marque).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php public function onSuccess(Brand $brand) { $this->em->persist($brand); if($brand->getId()) { /* SUPRESSION DES LIEUX */ $originalLocations = array(); foreach($this -> em -> getRepository('WinItAdminBundle:BrandLocation') -> findByBrand($brand -> getId()) as $location) { $originalLocations[] = $location; } foreach($originalLocations as $location) { foreach($brand->getLocations() as $key => $toDel) { if($toDel->getId() == $location->getId()) { unset($originalLocations[$key]); } } } foreach($originalLocations as $location){ $this->em->remove($location); } /* FIN SUPRESSION DES LIEUX */ } foreach($brand->getLocations() as $location) { $location->setModified = new \DateTime('now'); $brand->addLocation($location); $this->em->persist($location); } // JUSTE CE FOREACH NE MARCHE PAS, LE RESTE MARCHE foreach($brand->getUsers() as $user) { $brand->addUser($user); $this->em->persist($user); } $brand->setModified(new \DateTime('now')); $this->em->persist($brand); $this->em->flush(); }
J'ai fait des test sur cette boucle est l'on reçoit bien les marques cochées !
Je pense que le problème vient de la configuration des entités mais j'ai tout essayé également.
Si quelqu'un peut m'aider ... ça m'aiderait bien :-D
Merci à tous,
Gaëtan
Partager