2015-08-04 1 views
0

J'ai une relation un à plusieurs entre le prototype et l'image, un prototype peut avoir beaucoup d'images. J'ai essayé d'utiliser vich et voici ce que j'ai: Je peux télécharger des images mais pas en même temps. Je dois éditer, enregistrer et télécharger le second. De plus, je veux être en mesure de télécharger plusieurs images dans chaque section: bureau, tablette et mobile. Voici le code de mon PrototypeAdmin:Sonate admin Télécharger des images en utilisant vich

<?php 


namespace AppBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 

class PrototypeAdmin extends Admin 
{ 

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 
     ->with('Général') 
      ->add('nom', 'text', array('label' => 'Nom')) 
      ->add('description','text',array('label'=>'Description')) 
      ->add('dateCreation', 'date', array('label' => 'Date de création')) 

      ->add('projet','entity',array('class' => 'AppBundle\Entity\Projet')) 
     ->end() 

     ->with('Desktop') 
      ->add('images', 'sonata_type_collection', array('data_class' => null),array(
       'edit' => 'inline', 
       'inline' => 'table' 
      )) 
     ->end() 

     ->with('Tablette') 
      ->add('images', 'sonata_type_collection', array('data_class' => null),array(
       'edit' => 'inline', 
       'inline' => 'table' 
      )) 
     ->end() 

     ->with('Mobile') 
      ->add('images', 'sonata_type_collection', array('data_class' => null),array(
       'edit' => 'inline', 
       'inline' => 'table' 
      )) 
     ->end() 

     ->with('Dossier Complet') 
      ->add('file', 'file', array('required' => false , 'label' => 'Dossier complet')) 
     ->end() 
    ; 

} 


protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
{ 
    $datagridMapper 
     ->add('nom') 
     ->add('dateCreation') 
     ->add('projet.id') 
    ; 
} 


protected function configureListFields(ListMapper $listMapper) 
{ 

    $listMapper 
     ->add('nom') 
     ->add('description') 
     ->add('dateCreation') 
     ->add('_action', 'actions', array(
       'actions' => array(
       'show' => array(), 
       'delete' => array(), 
      ) 
     )) 

    ; 
} 
} 

Tout d'abord, je peux télécharger dans la section « Bureau » mais pas dans « Tablette » et « mobile ».

Alors voici mon ImageAdmin:

<?php 


namespace AppBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Form\FormMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 

class ImageAdmin extends Admin 
{ 

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 


      ->add('commentaire','text',array('label'=>'Commentaire')) 
      ->add('typeDevice', 'text', array('label' => 'Type de device')) 
      ->add('image', 'file', array('required' => false , 'label' => 'image')) 
      ->add('prototype','entity',array('class' => 'AppBundle\Entity\Prototype')) 


    ; 
} 


protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
{ 

} 


protected function configureListFields(ListMapper $listMapper) 
{ 

} 
} 

Voici mes deux entités:

Prototype.php:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* Prototype 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="AppBundle\Entity\PrototypeRepository") 
* @Vich\Uploadable 
* @ORM\HasLifecycleCallbacks 
*/ 
class Prototype 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="nom", type="string", length=255) 
*/ 
private $nom; 


/** 
* @var string 
* 
* @ORM\Column(name="description", type="string", length=255) 
*/ 
private $description; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="dateCreation", type="date") 
*/ 
private $dateCreation; 


/** 
* @ORM\Column(type="string", length=255, name="fichier_nom") 
* 
* @var string $nomFichier 
*/ 
public $nomFichier; 


/** 
* @ORM\Column(type="datetime") 
* 
* @var \DateTime $updatedAt 
*/ 
public $updatedAt; 


    /** 
    * Unmapped property to handle file uploads 
    * @Vich\UploadableField(mapping="prototype_fichier", fileNameProperty="nomFichier") 
    * 
    * @var File $file 
    */ 
private $file; 


/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Projet", inversedBy="prototypes") 
* @ORM\joinColumn(name="projet_id", referencedColumnName="id") 
*/ 
private $projet; 


    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Image", mappedBy="prototype",cascade={"persist"} , orphanRemoval=true) 
    * @ORM\OrderBy({"id"="ASC"}) 
    */ 
protected $images; 


public function __construct() 
{ 
    $this->images = new \Doctrine\Common\Collections\ArrayCollection(); 
    $this->dateCreation = new \DateTime("now"); 
    $this->nom = ""; 
    $this->description = " "; 

} 


/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 


/** 
* Get nom 
* 
* @return string 
*/ 
public function getNom() 
{ 
    return $this->nom; 
} 

public function __toString() 
{ 
return $this->getNom(); 
} 

/** 
* Set nom 
* 
* @param string $nom 
* @return Prototype 
*/ 
public function setNom($nom) 
{ 
    $this->nom = $nom; 

    return $this; 
} 


/** 
* Set description 
* 
* @param string $description 
* @return Prototype 
*/ 
public function setDescription($description) 
{ 
    $this->description = $description; 

    return $this; 
} 

/** 
* Get description 
* 
* @return string 
*/ 
public function getDescription() 
{ 
    return $this->description; 
} 

/** 
* Set dateCreation 
* 
* @param \DateTime $dateCreation 
* @return Prototype 
*/ 
public function setDateCreation($dateCreation) 
{ 
    $this->dateCreation = $dateCreation; 

    return $this; 
} 

/** 
* Get dateCreation 
* 
* @return \DateTime 
*/ 
public function getDateCreation() 
{ 
    return $this->dateCreation; 
} 


/** 
* Set projet 
* 
* @param \AppBundle\Entity\Projet $projet 
* @return Prototype 
*/ 
public function setProjet(\AppBundle\Entity\Projet $projet = null) 
{ 
    $this->projet = $projet; 
    return $this; 
} 

/** 
* Get projet 
* 
* @return \AppBundle\Entity\Projet 
*/ 
public function getProjet() 
{ 
    return $this->projet; 
} 



/** 
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
* 
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file 
*/ 
public function setFile(File $file = null) 
{ 
    $this->file = $file; 

    if ($file) { 

     $this->updatedAt = new \DateTime('now'); 
    } 
} 

/** 
* @return File 
*/ 
public function getFile() 
{ 
    return $this->file; 
} 

/** 
* @param string $nomFichier 
*/ 
public function setNomFichier($nomFichier) 
{ 
    $this->nomFichier = $nomFichier; 
} 

/** 
* @return string 
*/ 
public function getNomFichier() 
{ 
    return $this->nomFichier; 
} 



public function setImages($images) 
{ 
if (count($images) > 0) { 
    foreach ($images as $i) { 
     $this->addImages($i); 
    } 
} 

return $this; 
} 

/** 
* Add images 
* 
* @param \AppBundle\Entity\Image $images 
* @return Prototype 
*/ 
public function addImages(\AppBundle\Entity\Image $images) 
{ 
$this->images[]= $images; 
return $this; 
} 

public function addImage(\AppBundle\Entity\Image $image) 
{ 
$image->setPrototype($this); 
$this->images->add($image); 
} 


/** 
* Remove images 
* 
* @param \AppBunble\Entity\Image $images 
*/ 
public function removeImages(\AppBundle\Entity\Image $images) 
{ 
$this->images->removeElement($images); 
} 


/** 
* Get images 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 

public function getImages() 
{ 
return $this->images; 
} 
} 

Et Image.php

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* Prototype 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="AppBundle\Entity\PrototypeRepository") 
* @Vich\Uploadable 
* @ORM\HasLifecycleCallbacks 
*/ 
class Prototype 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="nom", type="string", length=255) 
*/ 
private $nom; 


/** 
* @var string 
* 
* @ORM\Column(name="description", type="string", length=255) 
*/ 
private $description; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="dateCreation", type="date") 
*/ 
private $dateCreation; 


/** 
* @ORM\Column(type="string", length=255, name="fichier_nom") 
* 
* @var string $nomFichier 
*/ 
public $nomFichier; 


/** 
* @ORM\Column(type="datetime") 
* 
* @var \DateTime $updatedAt 
*/ 
public $updatedAt; 


    /** 
    * Unmapped property to handle file uploads 
    * @Vich\UploadableField(mapping="prototype_fichier", fileNameProperty="nomFichier") 
    * 
    * @var File $file 
    */ 
private $file; 


/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Projet", inversedBy="prototypes") 
* @ORM\joinColumn(name="projet_id", referencedColumnName="id") 
*/ 
private $projet; 


/** 
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Image", mappedBy="prototype",cascade={"persist"} , orphanRemoval=true) 
* @ORM\OrderBy({"id"="ASC"}) 
*/ 
protected $images; 


public function __construct() 
{ 
    $this->images = new \Doctrine\Common\Collections\ArrayCollection(); 
    $this->dateCreation = new \DateTime("now"); 
    $this->nom = ""; 
    $this->description = " "; 

} 


/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 


/** 
* Get nom 
* 
* @return string 
*/ 
public function getNom() 
{ 
    return $this->nom; 
} 

public function __toString() 
{ 
return $this->getNom(); 
} 

/** 
* Set nom 
* 
* @param string $nom 
* @return Prototype 
*/ 
public function setNom($nom) 
{ 
    $this->nom = $nom; 

    return $this; 
} 


/** 
* Set description 
* 
* @param string $description 
* @return Prototype 
*/ 
public function setDescription($description) 
{ 
    $this->description = $description; 

    return $this; 
} 

/** 
* Get description 
* 
* @return string 
*/ 
public function getDescription() 
{ 
    return $this->description; 
} 

/** 
* Set dateCreation 
* 
* @param \DateTime $dateCreation 
* @return Prototype 
*/ 
public function setDateCreation($dateCreation) 
{ 
    $this->dateCreation = $dateCreation; 

    return $this; 
} 

/** 
* Get dateCreation 
* 
* @return \DateTime 
*/ 
public function getDateCreation() 
{ 
    return $this->dateCreation; 
} 


/** 
* Set projet 
* 
* @param \AppBundle\Entity\Projet $projet 
* @return Prototype 
*/ 
public function setProjet(\AppBundle\Entity\Projet $projet = null) 
{ 
    $this->projet = $projet; 
    return $this; 
} 

/** 
* Get projet 
* 
* @return \AppBundle\Entity\Projet 
*/ 
public function getProjet() 
{ 
    return $this->projet; 
} 



/** 
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
* 
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file 
*/ 
public function setFile(File $file = null) 
{ 
    $this->file = $file; 

    if ($file) { 

     $this->updatedAt = new \DateTime('now'); 
    } 
} 

/** 
* @return File 
*/ 
public function getFile() 
{ 
    return $this->file; 
} 

/** 
* @param string $nomFichier 
*/ 
public function setNomFichier($nomFichier) 
{ 
    $this->nomFichier = $nomFichier; 
} 

/** 
* @return string 
*/ 
public function getNomFichier() 
{ 
    return $this->nomFichier; 
} 



public function setImages($images) 
{ 
if (count($images) > 0) { 
    foreach ($images as $i) { 
     $this->addImages($i); 
    } 
} 

return $this; 
} 

/** 
* Add images 
* 
* @param \AppBundle\Entity\Image $images 
* @return Prototype 
*/ 
public function addImages(\AppBundle\Entity\Image $images) 
{ 
$this->images[]= $images; 
return $this; 
} 

public function addImage(\AppBundle\Entity\Image $image) 
{ 
$image->setPrototype($this); 
$this->images->add($image); 
} 


/** 
* Remove images 
* 
* @param \AppBunble\Entity\Image $images 
*/ 
public function removeImages(\AppBundle\Entity\Image $images) 
{ 
$this->images->removeElement($images); 
} 


/** 
* Get images 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 

public function getImages() 
{ 
return $this->images; 
} 

} 

I Je viens de commencer à utiliser S ymfony et Sonata Et peut-être qu'il y a une autre façon de le faire.

Edit:

Je viens de vérifier le mediaBundle, je suis suivant les étapes de la documentation. Ai-je besoin de générer les entités avec cette commande

php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle 

Ou peut-être que je peux simplement faire des changements dans mes propres entités?

+0

Je n'ai pas utilisé vichuploader depuis un moment, mais je me souviens qu'il ne pouvait pas gérer plusieurs téléchargements. – Hakim

Répondre

0

Votre question ne semble pas être liée directement avec VichUploaderBundle, vous avez probablement juste besoin d'ajouter deux options à vos images domaines: allow_add et allow_delete (tous deux mis à true). Vous pouvez également définir by_reference sur false.

J'ai implémenté ce que vous voulez réaliser en my sandbox.

+0

J'ai essayé de faire cela mais j'ai toujours le même problème: chaque fois que je clique sur 'ajouter' pour ajouter une autre image, la page se charge et je perds la première image en fait je dois encore les télécharger une par une. J'ai ceci: '-> add (' images ',' sonata_type_collection ', tableau ( ' type_options '=> array (' allow_add '=> true,' allow_delete '=> true,' by_reference '=> false)), array ('data_class' => null), array ( 'edit' => 'inline', 'inline' => 'table' )) ' –