Problème lier plusieurs images à un article (OnetoMany)
Bonjour,
J'ai un soucis concernant le fait de lier plusieurs images à un article.
Voici mes deux entitées:
Application.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
|
<?php
namespace GoSoft\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* GoSoft\MainBundle\Entity\Application
*
* @ORM\Table(name="applications")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="GoSoft\MainBundle\Entity\ApplicationRepository")
*/
class Application
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $nom
*
* @ORM\Column(name="nom", type="string", length=255)
* @Assert\MinLength(limit = 2)
*/
private $nom;
/**
* @ORM\OneToMany(targetEntity="GoSoft\MainBundle\Entity\Capture", mappedBy="application", cascade={"persist"})
*/
private $captures;
public function __construct()
{
$this->captures = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return Application
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* @param GoSoft\MainBundle\Entity\Capture $capture
* @return Application
*/
public function addCapture(\GoSoft\MainBundle\Entity\Capture $capture)
{
$this->captures[] = $capture;
return $this;
}
/**
* @param GoSoft\MainBundle\Entity\Capture $capture
*/
public function removeCapture(\GoSoft\MainBundle\Entity\Capture $capture)
{
$this->captures->removeElement($capture);
}
/**
* @return Doctrine\Common\Collections\Collection
*/
public function getCaptures()
{
return $this->captures;
} |
Capture.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 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
|
<?php
namespace GoSoft\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* GoSoft\MainBundle\Entity\Capture
*
* @ORM\Table(name="captures")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="GoSoft\MainBundle\Entity\CaptureRepository")
*/
class Capture
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $image
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @var object $objFile
* @Assert\File( maxSize = "5M", mimeTypesMessage = "Please upload a valid Image")
*/
private $objFile;
/**
* @ORM\ManyToOne(targetEntity="GoSoft\MainBundle\Entity\Application", inversedBy="captures")
* @ORM\JoinColumn(nullable=false)
*/
private $application;
public function __construct()
{
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set objFile
*
* @param object $objFile
* @return Capture
*/
public function setObjFile($objFile)
{
$this->objFile = $objFile;
return $this;
}
/**
* Get objFile
*
* @return object
*/
public function getObjFile()
{
return $this->objFile;
}
/**
* Set image
*
* @param string $image
* @return Capture
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
// Si le champ d'upload est vide on ne fait rien
if (null === $this->objFile) {
return;
}
// Si on ajoute une image
elseif (null !== $this->objFile) {
$this->image = $this->objFile->getClientOriginalName();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->objFile) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
// vous devez lancer une exception ici si le fichier ne peut pas
// être déplacé afin que l'entité ne soit pas persistée dans la
// base de données comme le fait la méthode move() de UploadedFile
$this->objFile->move($this->getUploadRootDir(), $this->image);
unset($this->objFile);
}
// propriété utilisé temporairement pour la suppression
private $filenameForRemove;
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->filenameForRemove = $this->getUploadRootDir().$this->image;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($this->filenameForRemove) {
unlink($this->filenameForRemove);
rmdir($this->filenameForRemove.'/..');
}
}
protected function getUploadRootDir()
{
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
// le document/image dans la vue.
return 'upload/capture_ecran/'.$this->getId().'/';
}
} |
Mon souci est que je créé un application avec un formulaire qui imbrique l'ajout de captures (mes images).
Le champ "application_id" dans la base Capture n'est pas mis à jour.
Je suis obligé de mettre le numero de l'appli à la main avec phpmyadmin.
Sauriez vous d'ou vient mon erreur?