Greetings to all. I was assigned to upgrade the existing site, which is written in Symfony 2. Now there is a list of news, you need to add a gallery to them. I created the "news_gallery" table:
- id integer NOT NULL [pk]
- title character varying (255)
- image character varying (255) NOT NULL
- id_news integer [fk]
Created a NewsGallery class:
namespace AppBundle\Entity; 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 $title; /** * @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; public function setImageFile(File $image = null) { $this->imageFile = $image; } public function getImageFile() { return $this->imageFile; } /** * @ORM\ManyToOne(targetEntity="News", inversedBy="news_gallerys") * @ORM\JoinColumn(name="id_news", referencedColumnName="id") */ public $news; /** * Set news * * @param \AppBundle\Entity\News $news * * @return news_gallerys */ public function setIdNews(\AppBundle\Entity\News $news) { $this->news = $news; return $this; } /** * Get news * * @return \AppBundle\Entity\News */ public function getIdNews() { return $this->news; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title * @return Image */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * 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; } } In the News class added a link:
namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; 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 { /** * @ORM\OneToMany(targetEntity="NewsGallerys", mappedBy="news") */ private $news_gallerys; /** * Add news_gallerys * * @param \AppBundle\Entity\NewsGallerys $news_gallerys * * @return News */ public function addNewsGallery(\AppBundle\Entity\NewsGallerys $news_gallerys) { $this->news_gallerys[] = $news_gallerys; return $this; } /** * Remove news_gallerys * * @param \AppBundle\Entity\NewsGallerys $news_gallerys */ public function removeNewsGallery(\AppBundle\Entity\NewsGallerys $news_gallerys) { $this->news_gallerys->removeElement($news_gallerys); } /** * Get news_gallerys * * @return \Doctrine\Common\Collections\Collection */ public function getNewsGallery() { return $this->news_gallerys; } .... } In the public part of the site in the template this gallery is displayed. But I have no idea how to generate this field in the form of adding news through the admin area. I reread a bunch of documentation and a bunch of forums, but I can't find anything.
Here is part of the config.yml file for News and the gallery itself:
News: label: app.menu.news class: AppBundle\Entity\News list: fields: - name - shortDescription show: fields: - name - shortDescription - fullDescription form: fields: - { property: 'created' } - name - shortDescription - fullDescription NewsGallerys: label: app.menu.news_gallery class: AppBundle\Entity\NewsGallerys list: fields: - title - { property: 'image', type: 'image', base_path: %app.path.newsgallery% } show: fields: - title - { property: 'image', type: 'image', base_path: %app.path.newsgallery% } form: fields: - title - { property: 'imageFile', type: 'vich_image' } How to add the "news_gallerys" property in News so that you can upload infinitely many photos?
Thank you for attention.