K
That's the solution. https://github.com/javiereguiluz/EasyAdminBundle/issues/825#issuecomment-212579578 ♪ He used "collection."That's what I end up with:
News.phpnamespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
News
@ORM\Table(name="news")
@ORM\Entity(repositoryClass="AppBundle\Repository\NewsRepository")
@ORM\HasLifecycleCallbacks()
@Vich\Uploadable
*/
class News
{
/**
@var datetime $created
@ORM\Column(type="datetime")
*/
protected $created;
/**
@return datetime
*/
public function getCreated() {
return $this->created;
}
public function setCreated($newdate) {
$this->created = $newdate;
return $this;
}
/**
@var datetime $updated
@ORM\Column(type="datetime", nullable = true)
*/
protected $updated;
/**
Gets triggered only on insert
@ORM\PrePersist
*/
public function onPrePersist()
{
$this->created = new \DateTime("now");
}
/**
Gets triggered every time on update
@ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updated = new \DateTime("now");
}
/**
@var int
@ORM\Column(name="id", type="integer")
@ORM\Id
@ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
@var string
@ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
@var string
@ORM\Column(name="shortDescription", type="string", length=1024)
*/
private $shortDescription;
/**
@var string
@ORM\Column(name="fullDescription", type="text")
*/
private $fullDescription;
/**
Get id
@return integer
*/
public function getId()
{
return $this->id;
}
/**
Set name
@param string $name
@return News
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
Get name
@return string
*/
public function getName()
{
return $this->name;
}
/**
Set shortDescription
@param string $shortDescription
@return News
*/
public function setShortDescription($shortDescription)
{
$this->shortDescription = $shortDescription;
return $this;
}
/**
Get shortDescription
@return string
*/
public function getShortDescription()
{
return $this->shortDescription;
}
/**
Set fullDescription
@param string $fullDescription
@return News
*/
public function setFullDescription($fullDescription)
{
$this->fullDescription = $fullDescription;
return $this;
}
/**
Get fullDescription
@return string
*/
public function getFullDescription()
{
return $this->fullDescription;
}
/**
@var string
@ORM\Column(name="image", type="string", length=1024)
*/
private $image;
/**
Set image
@param string $image
@return Image
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
Get image
@return string
*/
public function getImage()
{
return $this->image;
}
/**
@Vich\UploadableField(mapping="newsgallery", fileNameProperty="image")
@var File
*/
private $imageFile;
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
}
public function getImageFile()
{
return $this->imageFile;
}
/**
@ORM\OneToMany(targetEntity="NewsGallerys", mappedBy="news")
/
private $news_gallerys;
public function __construct() {
$this->news_gallerys = new ArrayCollection();
}
public function __toString()
{
return (string)$this->name;
}
/*
Add news_gallerys
@param \AppBundle\Entity\NewsGallerys $news_gallerys
@return News
*/
public function addNewsGallerys(\AppBundle\Entity\NewsGallerys $news_gallerys)
{
$this->news_gallerys[] = $news_gallerys;
return $this;
}
/**
Remove news_gallerys
@param \AppBundle\Entity\NewsGallerys $news_gallerys
*/
public function removeNewsGallerys(\AppBundle\Entity\NewsGallerys $news_gallerys)
{
$this->news_gallerys->removeElement($news_gallerys);
}
/**
Get news_gallerys
@return NewsGallerys[]|ArrayCollection
*/
public function getNewsGallerys()
{
return $this->news_gallerys;
}
}
File NewsGallerys.phpnamespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
Image
@ORM\Table(name="news_gallery")
@ORM\Entity(repositoryClass="AppBundle\Repository\NewsGallerysRepository")
@Vich\Uploadable
/
class NewsGallerys
{
/*
@var int
@ORM\Column(name="id", type="integer")
@ORM\Id
@ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
@var string
@ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $name;
/**
@var string
@ORM\Column(name="image", type="string", length=1024)
*/
private $image;
/**
@Assert\File(
maxSize="2m",
mimeTypes={"image/png", "image/jpeg"}
)
@Vich\UploadableField(mapping="newsgallery", fileNameProperty="image")
@var File
*/
private $imageFile;
/**
Get id
@return integer
*/
public function getId()
{
return $this->id;
}
/**
Set title
@param string $title
@return NewsGallerys
*/
public function setName($name)
{
$this->title = $name;
return $this;
}
/**
Get title
@return string
*/
public function getName()
{
return $this->name;
}
/**
Set image
@param File|null $image
@return NewsGallerys
/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
return $this;
}
/*
@return File
/
public function getImageFile()
{
return $this->imageFile;
}
/*
@param string $image
@return NewsGallerys
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
Get image
@return string
*/
public function getImage()
{
return $this->image;
}
/**
@ORM\ManyToOne(targetEntity="News",cascade={"persist", "remove"},inversedBy="news_gallerys")
@ORM\JoinColumn(name="id_news",referencedColumnName="id",nullable=true)
*/
public $news;
public function __toString(){
return (string) $this->name;
}
}
at config.yml, for the essence of News, wrote such fields:News:
label: app.menu.news
class: AppBundle\Entity\News
list:
fields:
name
shortDescription
show:
fields:
name
shortDescription
fullDescription
{ property: 'image', type: 'image', base_path: %app.path.newsgallery% }
form:
fields:
{ property: 'created' }
name
shortDescription
fullDescription
{ property: 'imageFile', type: 'vich_image' }
{ property: 'news_gallerys', type: 'collection', type_options: { entry_type: 'AppBundle\Form\ImageType' } }
Then created ImageType.php:namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('imageFile', VichFileType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\NewsGallerys'
));
}
}
As a result, I get a multiple addition of files with a type of picture, if they're already added to the database, it's correct, but I can't add a new one or delete it. Makes a mistake:Request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMInvalidArgumentException: "A new entity was found through the relationship 'AppBundle/Entity/News#news_gallerys mapping entity' that was not configured to cascade persist operations for entity: .If anyone tells me how to decide it, I'll be grateful.