0

我在原型和图像之间有一对多的关系,一个原型可以有很多图像。我尝试使用 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 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(),
            )
        ))

    ;
}
}

首先,我可以在“桌面”部分上传,但不能在“平板电脑”和“手机”部分上传。

然后这是我的 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)
{   

}
}

这是我的两个实体:

原型.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

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;
}

}

我刚刚开始使用 Symfony 和 Sonata,也许还有另一种方法可以做到这一点。

编辑:

我刚刚检查了 mediaBundle,我正在按照文档中的步骤进行操作。我是否需要使用此命令生成实体

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

或者也许我可以对自己的实体进行更改?

4

1 回答 1

0

您的问题似乎与 没有直接关系VichUploaderBundle,您可能只需要在您的images字段中添加两个选项:allow_addallow_delete(都设置为true)。您可能还想设置by_referencefalse.

我在我的沙箱中实现了您想要实现的目标。

于 2015-08-05T11:59:54.897 回答