Symfony 3 - Soucis entités
Bonjour à tous,
Je suis en train de me consacrer à l'apprentissage de Symfony 3 mais je bloque pour un truc tout con.
Je m'explique, j'ai donc un site de développement tout con plutôt très proche de l'exemple donné dans le cours de Symfony 2 sur OCR, j'ai donc :
- Des annonces
- Des catégories
- Des photos
Chaque annonce doit appartenir à une seule et unique catégories, une annonce peut avoir plusieurs photos et une photo est reliée à une seule annonce.
Mon soucis, lorsque je veux créer une nouvelle annonce j'ai droit à ce message :
Citation:
A new entity was found through the relationship 'Adoptez\AnimalsBundle\Entity\Advert#pictures' that was not configured to cascade persist operations for entity: Adoptez\AnimalsBundle\Entity\Pictures@00000000503b70d900000000e3ce266f. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Adoptez\AnimalsBundle\Entity\Pictures#__toString()' to get a clue.
Voici donc mes entités Adverts et pictures (category fonctionnant bien)
Adverts :
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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| <?php
namespace Adoptez\AnimalsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Advert
*
* @ORM\Table(name="advert")
* @ORM\Entity(repositoryClass="Adoptez\AnimalsBundle\Repository\AdvertRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Advert
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/** @ORM\ManyToOne(targetEntity="Adoptez\AnimalsBundle\Entity\Category", inversedBy="advert")
* @ORM\JoinColumn(name="categories_id", referencedColumnName="id")
*/
private $category;
/**
* @ORM\OneToMany(targetEntity="Adoptez\AnimalsBundle\Entity\Pictures", mappedBy="advert", cascade={"persist", "all"})
*/
private $pictures;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="date", nullable=true)
*/
private $birthday;
/**
* @var int
*
* @ORM\Column(name="published", type="boolean", nullable=true)
*/
private $published = true;
/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updated_at;
public function __construct()
{
$this->pictures = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Advert
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Advert
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set birthday
*
* @param \DateTime $birthday
*
* @return Advert
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set published
*
* @param boolean $published
*
* @return Advert
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* @return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* Set updated_at
*
* @param \DateTime $updated_at
*
* @return Advert
*/
public function setUpdatedAt($updated_at)
{
$this->updated_at = $updated_at;
return $this;
}
/**
* @ORM\PreUpdate
*/
public function updatedDate()
{
$this->setUpdatedAt(new \Datetime());
}
public function addPictures(Pictures $pictures)
{
$this->pictures[] = $pictures;
return $this;
}
public function removePictures(Pictures $pictures)
{
$this->pictures->removeElement($pictures);
}
public function getPictures()
{
return $this->pictures;
}
public function addCategory(Category $category)
{
$this->category = $category;
return $this;
}
public function removeCategory(Category $category)
{
$this->$category->removeElement($category);
}
public function getCategory()
{
return $this->category;
}
} |
Pictures :
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 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 227 228 229 230 231 232 233 234 235 236 237
| <?php
namespace Adoptez\AnimalsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Pictures
*
* @ORM\Table(name="pictures")
* @ORM\Entity(repositoryClass="Adoptez\AnimalsBundle\Repository\PicturesRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Pictures
{
/**
* @ORM\ManyToOne(targetEntity="Adoptez\AnimalsBundle\Entity\Advert", inversedBy="pictures", cascade={"persist"})
* @ORM\JoinColumn(name="advert_id", referencedColumnName="id")
*/
private $advert;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="extension", type="string", length=255)
*/
private $extension;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer", nullable=true)
*/
private $position;
private $tempFilename;
private $file;
public function setAdvert(Advert $advert)
{
$this->advert = $advert;
return $this;
}
public function getAdvert()
{
return $this->advert;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set extension
*
* @param string $extension
*
* @return Pictures
*/
public function setExtension($extension)
{
$this->extension = $extension;
return $this;
}
/**
* Get extension
*
* @return string
*/
public function getExtension()
{
return $this->extension;
}
/**
* Set alt
*
* @param string $alt
*
* @return Pictures
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
/**
* Set position
*
* @param integer $position
*
* @return Pictures
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* @return integer
*/
public function getPosition()
{
return $this->position;
}
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (null !== $this->extension) {
$this->tempFilename = $this->extension;
$this->extension = null;
$this->alt = null;
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload(){
if (null === $this->file) {
return;
}
$this->extension = $this->file->guessExtension();
$this->alt = $this->file->getClientOriginalName();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->extension
);
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->extension;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFilename)) {
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 relatif vers l'image pour notre code PHP
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
} |
Merci d'avance pour votre aide !
Bonne journée