Salut tout le monde j'ai un petit soucis de lien entre entité au moment de la soumission d'un formulaire.

J'ai deux entités image et catégorie en relation OneToOne, par soucis de lisibilité je ne vous met pas le code juste l'erreur car je pense que c'est un problème de base pour dees personnes qui ont déjà fais du Symfony mais s'il y a besoin de peu publié le code...

Donc vla l'erreur : Found entity of type Doctrine\Common\Collections\ArrayCollection on association Echyzen\NewsBundle\Entity\Image#rubrique, but expecting Echyzen\NewsBundle\Entity\Rubrique

ah je précise que le doctrine:...:validate est correct

et voila le code :

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
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
<?php
// src/Echyzen/NewsBundle/Entity/Image.php
namespace Echyzen\NewsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
 
/**
* @ORM\Entity(repositoryClass="Echyzen\NewsBundle\Entity\ImageRepository")
* @ORM\HasLifecycleCallbacks
*/
class Image
{
    /**
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;
    /**
    * @ORM\Column(name="url", type="string", length=255)
    */
    private $url;
    /**
    * @ORM\Column(name="alt", type="string", length=255)
    */
    private $alt;
 
    /**
    * @ORM\OneToOne(targetEntity="Echyzen\NewsBundle\Entity\Rubrique", mappedBy="image")
    */
    private $rubrique;
 
    /**
    * @Assert\File(maxSize="500k")
    *
    * @var UploadedFile
    */
    private $file;
 
    private $tempFilename;
 
    /**
    * @ORM\PrePersist()
    * @ORM\PreUpdate()
    */
    public function preUpload()
    {
        // Si jamais il n'y a pas de fichier (champ facultatif)
        if (null === $this->file) {
            return;
        }
 
        // Le nom du fichier est son id, on doit juste stocker également son extension
        $this->url = $this->file->guessExtension();
        // Et on génère l'attribut alt de la balise <img>, à  la valeur du nom du fichier
        $this->alt = $this->file->getClientOriginalName();
 
    }
 
    /**
    * @ORM\PostPersist()
    * @ORM\PostUpdate()
    */
    public function upload()
    {
 
        // Si jamais il n'y a pas de fichier (champ facultatif)
        if (null === $this->file) {
            return;
        }
 
        // Si on avait un ancien fichier, on le supprime
        if (null !== $this->tempFilename) {
            $oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
            if (file_exists($oldFile)) {
                unlink($oldFile);
            }
        }
        // On déplace le fichier envoyée dans le répertoire de notre choix
        $this->file->move(
            $this->getUploadRootDir(), // Le répertoire de destination
            $this->id.'.'.$this->url // Le nom du fichier à  créer, ici "id.extension"
        );
    }
 
    /**
    * @ORM\PreRemove()
    */
    public function preRemoveUpload()
    {
        // On sauvegarde temporairement le nom du fichier car il dépend de l'id
        $this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->url;
    }
 
    /**
    * @ORM\PostRemove()
    */
    public function removeUpload()
    {
        // En PostRemove, on n'a pas accès à  l'id, on utilise notre nom sauvegardée
        if (file_exists($this->tempFilename)) {
            // On supprime le fichier
            unlink($this->tempFilename);
        }
    }
    public function getUploadDir()
    {
        // On retourne le chemin relatif vers l'image pour un navigateur
        return 'uploads/img';
    }
 
    protected function getUploadRootDir()
    {
        // On retourne le chemin absolu vers l'image pour notre code PHP
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }
 
    public function getWebPath()
    {
        return $this->getUploadDir().'/'.$this->getId().'.'.$this->getUrl();
    }
 
    /**
    * @return integer
    */
    public function getId()
    {
        return $this->id;
    }
 
    /**
    * @param string $url
    * @return Image
    */
    public function setUrl($url)
    {
        $this->url = $url;
        return $this;
    }
 
    /**
    * @return string
    */
    public function getUrl()
    {
        return $this->url;
    }
 
    /**
    * @param string $alt
    * @return Image
    */
    public function setAlt($alt)
    {
        $this->alt = $alt;
        return $this;
    }
 
    /**
    * @return string
    */
    public function getAlt()
    {
        return $this->alt;
    }
 
    public function setFile($file)
    {
        $this->file = $file;
        // On vérifie si on avait déjà  un fichier pour cette entitée
        if (null !== $this->url) {
            // On sauvegarde l'extension du fichier pour le supprimer plus tard
            $this->tempFilename = $this->url;
            // On réinitialise les valeurs des attributs url et alt
            $this->url = null;
            $this->alt = null;
        }
    }
 
    public function getFile()
    {
        return $this->file;
    }
 
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->rubrique = new \Doctrine\Common\Collections\ArrayCollection();
    }
 
    /**
     * Add rubrique
     *
     * @param \Echyzen\NewsBundle\Entity\Rubrique $rubrique
     * @return Image
     */
    public function addRubrique(\Echyzen\NewsBundle\Entity\Rubrique $rubrique)
    {
        $this->rubrique[] = $rubrique;
 
        return $this;
    }
 
    /**
     * Remove rubrique
     *
     * @param \Echyzen\NewsBundle\Entity\Rubrique $rubrique
     */
    public function removeRubrique(\Echyzen\NewsBundle\Entity\Rubrique $rubrique)
    {
        $this->rubrique->removeElement($rubrique);
    }
 
    /**
     * Get rubrique
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getRubrique()
    {
        return $this->rubrique;
    }
}
Pour l'entité rubrique :
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
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
<?php
 
namespace Echyzen\NewsBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * Rubrique
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Echyzen\NewsBundle\Entity\RubriqueRepository")
 */
class Rubrique
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     *
     * @Assert\Length(min=2, minMessage="La rubrique doit faire au moins {{limit}} caractères")
     */
    private $nom;
 
 
    /**
    * @ORM\OneToMany(targetEntity="Echyzen\NewsBundle\Entity\News", mappedBy="rubrique")
    */
    private $news;
 
    /**
    * @ORM\OneToOne(targetEntity="Echyzen\NewsBundle\Entity\Image", cascade={"persist", "remove"}, inversedBy="rubrique")
    * @ORM\JoinColumn(nullable=false)
    * @Assert\NotBlank()
    */
    private $image;
 
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
 
    /**
     * Set nom
     *
     * @param string $nom
     * @return Rubrique
     */
    public function setNom($nom)
    {
        $this->nom = $nom;
 
        return $this;
    }
 
    /**
     * Get nom
     *
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->news = new \Doctrine\Common\Collections\ArrayCollection();
    }
 
    /**
     * Add news
     *
     * @param \Echyzen\NewsBundle\Entity\News $news
     * @return Rubrique
     */
    public function addNews(\Echyzen\NewsBundle\Entity\News $news)
    {
        $this->news[] = $news;
 
        return $this;
    }
 
    /**
     * Remove news
     *
     * @param \Echyzen\NewsBundle\Entity\News $news
     */
    public function removeNews(\Echyzen\NewsBundle\Entity\News $news)
    {
        $this->news->removeElement($news);
    }
 
    /**
     * Get news
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getNews()
    {
        return $this->news;
    }
 
    /**
     * Set image
     *
     * @param \Echyzen\NewsBundle\Entity\Image $image
     * @return Rubrique
     */
    public function setImage(\Echyzen\NewsBundle\Entity\Image $image)
    {
        $this->image = $image;
 
        return $this;
    }
 
    /**
     * Get image
     *
     * @return \Echyzen\NewsBundle\Entity\Image
     */
    public function getImage()
    {
        return $this->image;
    }
}
Pour le formulaire Type Image :
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
<?php
 
namespace Echyzen\NewsBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
class ImageType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', 'file')
        ;
    }
 
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Echyzen\NewsBundle\Entity\Image'
        ));
    }
 
    /**
     * @return string
     */
    public function getName()
    {
        return 'echyzen_newsbundle_image';
    }
}
Pour le formulaire type Rubrique :
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
<?php
 
namespace Echyzen\NewsBundle\Form;
 
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
class RubriqueType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('image', new ImageType())
        ;
    }
 
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Echyzen\NewsBundle\Entity\Rubrique'
        ));
    }
 
    /**
     * @return string
     */
    public function getName()
    {
        return 'echyzen_newsbundle_rubrique';
    }
}
Merci d'avance de votre aide