upload dans les form collections
Bonjour, y a-t-il une manière de faire marcher l'upload de fichier avec les form collections ? J'ai suivi documentation et ca a bien fonctionné. Maintenant j'ai deux entités Organisme et Photo en relations One to Many unidirectional with join table. J'essai d'ajouter plusieurs photos a un organisme directement dans le formulaire de création ou d'édition d'un organisme.
Voici mes entités
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
|
<?php
namespace Svi\FonctionBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Iphp\FileStoreBundle\Mapping\Annotation as FileStore;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Photo
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Svi\FonctionBundle\Repository\PhotoRepository")
* Gedmo\TranslationEntity(class="Svi\FonctionBundle\Entity\Translation\PhotoTranslation")
*/
class Photo
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="titre", type="string", length=255)
* @Gedmo\Translatable
*/
private $titre;
/**
* @var string
* @FileStore\UploadableField(mapping="photo")
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
* @Gedmo\Translatable
*/
private $description;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* @param string $titre
* @return Photo
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* @return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set image
*
* @param string $image
* @return Photo
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set description
*
* @param string $description
* @return Photo
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->image)) {
// store the old name to delete after the update
$this->temp = $this->image;
$this->image = null;
} else {
$this->image = 'initial';
}
}
public function getAbsolutePath()
{
return null === $this->image
? null
: $this->getUploadRootDir().'/'.$this->image;
}
public function getWebPath()
{
return null === $this->image
? null
: $this->getUploadDir().'/'.$this->image;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/photo';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
//$filename = sha1(uniqid(mt_rand(), true));
$filename = $this->getFile()->getClientOriginalName();
$this->image = $filename.'.'.$this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->image);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
} |
le fragment pertinent de mon OrganismeType:
Code:
1 2 3 4 5 6 7 8 9
|
$builder
->add('photos', 'collection', array(
'type' => new PhotoType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
; |
et le PhotoType()
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
|
class PhotoOrganismeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre')
->add('file', 'file')
->add('description')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Svi\FonctionBundle\Entity\Photo'
));
}
public function getName()
{
return 'svi_fonctionbundle_phototype';
}
} |
Le seul problème que j'ai est que quand j'envoie le formulaire, tous les champs sont persistés à l'exception du champ image de l'entité photo. Les images ne sont pas non plus enregistré dans le dossier cible. Est-ce que j'ai manqué une étape ? Merci de votre aide.