我在原型和图像之间有一对多的关系,一个原型可以有很多图像。 我试过用vich,这就是我所拥有的: 我可以上传图片但不能同时上传。我必须编辑,保存然后上传第二个。 此外,我希望能够在每个部分上传多个图像:桌面,平板电脑和移动设备。 以下是My PrototypeAdmin的代码:
<?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)
{
}
}
首先,我可以在“桌面”部分上传,但不能在“Tablette”和“移动”中上传。
然后这是我的ImageAdmin:
<?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;
}
}
这是我的两个实体:
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;
}
}
和Image.php
php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle
我刚刚开始使用Symfony和Sonata而且还有另外一种方法可以做到这一点。
修改
我刚检查了mediaBundle,我正在按照文档中的步骤进行操作。我是否需要使用此命令生成实体
{{1}}
或许我可以在自己的实体中进行更改?
答案 0 :(得分:0)
您的问题似乎与VichUploaderBundle
没有直接关系,您可能只需要为images
字段添加两个选项:allow_add
和allow_delete
(两者都设置)到true
)。您可能还想将by_reference
设置为false
。
我在my sandbox中实现了您想要实现的目标。