アップロードした画像を VichUploaderBundle で削除または編集できません。OneToMany (双方向の関係) を持つ Annonce エンティティと Photo エンティティがあります。属性 setUpdatedAt を使用して vich prePersist を呼び出そうとしましたが、機能しません。
ここにアナウンスがあります:
class Annonce
{
// ...
/**
* @ORM\OneToMany(targetEntity="Immo\AnnonceBundle\Entity\Photo", mappedBy="annonce", cascade={"persist", "remove"})
*/
private $photos;
setter/getterImage() を使用した Photo エンティティ:
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Photo
* @Vich\Uploadable
* @ORM\Table()
*/
class Photo
{ // id, etc.
/**
* @Assert\File(
* maxSize="1M",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
* )
* @Vich\UploadableField(mapping="uploads_image", fileNameProperty="url")
*
* @var File $image
*/
protected $image;
/**
* @ORM\Column(type="string", length=255, name="url")
*
* @var string $url
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="Immo\AnnonceBundle\Entity\Annonce", inversedBy="photos")
*/
private $annonce;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime $updatedAt
*/
protected $updatedAt;
/**
* Set image
*
* @param string $image
* @return Photo
*/
public function setImage($image)
{
$this->image = $image;
if ($this->image instanceof UploadedFile) {
$this->updatedAt = new \DateTime('now');
}
return $this;
}
これが私のconfig.ymlです:
knp_gaufrette:
stream_wrapper: ~
adapters:
uploads_adapter:
local:
directory: %kernel.root_dir%/../web/img/uploads
filesystems:
uploads_image_fs:
adapter: uploads_adapter
vich_uploader:
db_driver: orm
twig: true
gaufrette: true
storage: vich_uploader.storage.gaufrette
mappings:
uploads_image:
delete_on_remove: true
delete_on_update: true
inject_on_load: true
uri_prefix: img/uploads
upload_destination: uploads_image_fs
namer: vich_uploader.namer_uniqid
私のアナウンスタイプ:
$builder->add('photos', 'collection', array('type' => new PhotoType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
)
)
写真の種類:
$builder->add('image', 'file')
コントローラー:
public function updateAnnonceAction($id)
{
$em = $this->getDoctrine()->getManager();
$annonce = $em->getRepository('ImmoAnnonceBundle:Annonce')
->findCompleteAnnonceById($id);
$form = $this->createForm(new AnnonceType, $annonce);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($annonce);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'ok');
return $this->redirect($this->generateUrl('immo_admin_annonce_homepage'));
}
}
return $this->render('ImmoAnnonceBundle:Admin/Annonce:update.html.twig', array('annonce' => $annonce,
'form' => $form->createView()
)
);
}
そして、私のテンプレートは、各写真の入力ファイルを html の Annonce に置きます:
{{ form_widget(form.photos) }} // With JS to manage add/delete on each input.
// Return this :
<input type="file" required="required" name="immo_annoncebundle_annonce[photos][2][image]" id="immo_annoncebundle_annonce_photos_2_image">