Upload image, problème 'move'
Bonjour,
Je débute avec symfony 2.
J'ai suivi quelques tutos afin de réaliser un petit site pour me former.
J'ai 2 entités : News et Image avec une relation onetoone.
J'ai créé mes formulaires afin d'ajouter une news et une image associée.
Ca fonctionne très bien, sauf que dans ma table image j'ai une url du genre "c:\wamp\tmp\phptmp545.bmp". Normal vu que j'ai pas traité le cas upload.
En regardant la doc symfony, il y a une méthode 'move' qui permet cela. Sauf que j'obtiens une erreur.
Méthodes utiles de l'entité News :
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
| public function getFullImagePath() {
return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}
protected function getTmpUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../web/upload/';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
// move takes the target directory and then the target filename to move to
$this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
// set the path property to the filename where you'ved saved the file
$this->image->url = $this->image->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->image = null;
} |
La fonction où j'appelle ma méthode upload (NewsHandler) :
Code:
1 2 3 4 5 6 7
| public function onSuccess(News $news)
{
$this->em->persist($news);
$news->upload();
$this->em->persist($news->getImage());
$this->em->flush();
} |
A ce niveau, mon formulaire est créé, la méthode POST vérifié, donc je suppose que l'objet News est rempli, ainsi que son image rattachée.
J'obtiens cette erreur :
Code:
Fatal error: Call to undefined method Feria\NewsBundle\Entity\Image::move() in C:\wamp\www\Symfony\src\Feria\NewsBundle\Entity\News.php on line 285
Où la ligne 285 est la ligne où j'appelle move.
$this->image est bien une propriété de la classe courante, et est non null.
Si je suis pas clair, n'hésitez pas à me demander.
Merci d'avance.