[Symfony2] Problème de persistance Rel ManytoMany
Bonjour tout le monde,
voila je me casse la tete depuis quelque jours mais pas de resultat, le probleme est le suivant :
j'ai deux entités, Radios et TypeSongs qui sont en relation MANYTOMANY le schéma est validé par la console et tout fonctionne parfaitement jusqu'au moment ou j'ai décidé de générer un formulaire pour enregistrer l'entité radio a savoir que j'ai enregistrer plusieurs entités TypeSongs bref, lorsque je remplie mon fromulaire RadioType et je choisi une ou plusieurs TypeSongs puis j'enregistre la table intermédiaire (typesongs_radios) que doctrine génère pour notre relation MANYTOMANY reste vierge o_O
je vous présente le code
RadioType.php
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 musicana\AlbumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RadiosType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('types', 'entity', array(
'class' => 'musicanaAlbumBundle:TypeSongs',
'property' => 'typeName',
'multiple' => true))
->add('radioName', 'text')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'musicana\AlbumBundle\Entity\Radios'
));
}
/**
* @return string
*/
public function getName()
{
return 'musicana_albumbundle_radios';
}
} |
Radio.php
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
| <?php
namespace musicana\AlbumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Radios
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="musicana\AlbumBundle\Entity\RadiosRepository")
*/
class Radios
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="radio_name", type="string", length=255)
*/
private $radioName;
/**
* @ORM\ManyToMany(targetEntity="musicana\AlbumBundle\Entity\TypeSongs", mappedBy="radio", cascade={"persist"})
*/
private $types; // Ici types prend un « s », car un radio peut comprendre plusieurs types !
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set radioName
*
* @param string $radioName
* @return Radios
*/
public function setRadioName($radioName)
{
$this->radioName = $radioName;
return $this;
}
/**
* Get radioName
*
* @return string
*/
public function getRadioName()
{
return $this->radioName;
}
/**
* Constructor
*/
public function __construct()
{
$this->types = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add types
*
* @param \musicana\AlbumBundle\Entity\TypeSongs $types
* @return Radios
*/
public function addType(\musicana\AlbumBundle\Entity\TypeSongs $types)
{
$this->types[] = $types;
$types->setRadio($this);
return $this;
}
/**
* Remove types
*
* @param \musicana\AlbumBundle\Entity\TypeSongs $types
*/
public function removeType(\musicana\AlbumBundle\Entity\TypeSongs $types)
{
$this->types->removeElement($types);
}
/**
* Get types
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTypes()
{
return $this->types;
}
} |
TypeSongs.php
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
| <?php
namespace musicana\AlbumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Type_songs
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="musicana\AlbumBundle\Entity\TypeSongsRepository")
*/
class TypeSongs
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="type_name", type="string", length=255)
*/
private $typeName;
/**
* @ORM\ManyToMany(targetEntity="musicana\AlbumBundle\Entity\Radios", inversedBy="types", cascade={"persist"})
*/
private $radio; // si on enleve lannotation l'affichage foncionnera
/**
* @ORM\OneToMany(targetEntity="musicana\AlbumBundle\Entity\Songs", mappedBy="type")
*/
private $songs; // Ici songs prend un « s », car un type de songs a plusieurs songs !
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set typeName
*
* @param string $typeName
* @return Type_songs
*/
public function setTypeName($typeName)
{
$this->typeName = $typeName;
return $this;
}
/**
* Get typeName
*
* @return string
*/
public function getTypeName()
{
return $this->typeName;
}
/**
* Set radio
*
* @param \musicana\AlbumBundle\Entity\Radios $radio
* @return Type_songs
*/
public function setRadio(\musicana\AlbumBundle\Entity\Radios $radio = null)
{
$this->radio = $radio;
return $this;
}
/**
* Get radio
*
* @return \musicana\AlbumBundle\Entity\Radios
*/
public function getRadio()
{
return $this->radio;
}
/**
* Constructor
*/
public function __construct()
{
$this->songs = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add songs
*
* @param \musicana\AlbumBundle\Entity\Songs $songs
* @return Type_songs
*/
public function addSong(\musicana\AlbumBundle\Entity\Songs $songs)
{
$this->songs[] = $songs;
$songs->setType($this);
return $this;
}
/**
* Remove songs
*
* @param \musicana\AlbumBundle\Entity\Songs $songs
*/
public function removeSong(\musicana\AlbumBundle\Entity\Songs $songs)
{
$this->songs->removeElement($songs);
}
/**
* Get songs
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSongs()
{
return $this->songs;
}
/**
* Add radio
*
* @param \musicana\AlbumBundle\Entity\Radios $radio
* @return TypeSongs
*/
public function addRadio(\musicana\AlbumBundle\Entity\Radios $radio)
{
$this->radio[] = $radio;
return $this;
}
/**
* Remove radio
*
* @param \musicana\AlbumBundle\Entity\Radios $radio
*/
public function removeRadio(\musicana\AlbumBundle\Entity\Radios $radio)
{
$this->radio->removeElement($radio);
}
} |
RadioController.php
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
| <?php
namespace musicana\AlbumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use musicana\AlbumBundle\Entity\Radios;
use musicana\AlbumBundle\Form\RadiosType;
class RadioController extends Controller
{
public function ajouterRadioAction()
{
$radio = new Radios;
$form = $this->createForm(new RadiosType, $radio);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($radio);
$em->flush();
return $this->redirect($this->generateUrl('musicana_ajouter_radios'));
}
}
return $this->render('musicanaAlbumBundle:Default:RadioForm.html.twig', array(
'form' => $form->createView(),
));
}
} |
Rq: Symfony ne me génère aucune erreur :cry:
Merci d'avance pour votre aide