Partage
  • Partager sur Facebook
  • Partager sur Twitter

Insertion many to Many symfony4

25 janvier 2020 à 11:24:23

Bonjour j'ai une relation entre gallery et category many to many et 

j'ai crée un formulaire nom,image et je dois avoir aussi une liste des catégories

j'ai ajouté dans le galleryType 

mais j'ai une erreur could not be converted to string

  ->add('categoryGalleries', EntityType::class, array(
                'class' => CategoryGallery::class,
                'multiple' => true,
                'expanded' => true,

            ))



  • Partager sur Facebook
  • Partager sur Twitter
25 janvier 2020 à 20:51:16

bonsoir merci pour votre réponse mais toujours ça ne fonctionne pas 

voila mes deux entity 

Entity catégorie

class CategoryGallery
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\GalleryVideo", inversedBy="categoryGalleries")
     */
    private $gallery_video;

    public function __construct()
    {
        $this->gallery_video = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->nam;
    }

    public function setName(string $nam): self
    {
        $this->nam = $nam;

        return $this;
    }

    /**
     * @return Collection|GalleryVideo[]
     */
    public function getGalleryVideo(): Collection
    {
        return $this->gallery_video;
    }

    public function addGalleryVideo(GalleryVideo $galleryVideo): self
    {
        if (!$this->gallery_video->contains($galleryVideo)) {
            $this->gallery_video[] = $galleryVideo;
        }

        return $this;
    }

    public function removeGalleryVideo(GalleryVideo $galleryVideo): self
    {
        if ($this->gallery_video->contains($galleryVideo)) {
            $this->gallery_video->removeElement($galleryVideo);
        }

        return $this;
    }
}

Entity gallery video 

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\GalleryVideoRepository")
 */
class GalleryVideo
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

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

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

    /**
     * @ORM\Column(type="datetime")
     */
    private $Created_at;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\CategoryGallery", mappedBy="gallery_video")
     */
    private $categoryGalleries;

    public function __construct()
    {
        $this->categoryGalleries = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getOtherInformation(): ?string
    {
        return $this->other_information;
    }

    public function setOtherInformation(string $other_information): self
    {
        $this->other_information = $other_information;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(string $url): self
    {
        $this->url = $url;

        return $this;
    }

    public function getPictureCover()
    {
        return $this->picture_cover;
    }

    public function setPictureCover( $picture_cover)
    {
        $this->picture_cover = $picture_cover;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->Created_at;
    }

    public function setCreatedAt(\DateTimeInterface $Created_at): self
    {
        $this->Created_at = $Created_at;

        return $this;
    }

    /**
     * @return Collection|CategoryGallery[]
     */
    public function getCategoryGalleries(): Collection
    {
        return $this->categoryGalleries;
    }

    public function addCategoryGallery(CategoryGallery $categoryGallery): self
    {
        if (!$this->categoryGalleries->contains($categoryGallery)) {
            $this->categoryGalleries[] = $categoryGallery;
            $categoryGallery->addGalleryVideo($this);
        }

        return $this;
    }

    public function removeCategoryGallery(CategoryGallery $categoryGallery): self
    {
        if ($this->categoryGalleries->contains($categoryGallery)) {
            $this->categoryGalleries->removeElement($categoryGallery);
            $categoryGallery->removeGalleryVideo($this);
        }

        return $this;
    }
}

et dans le GAlleryVideoType

      ->add('gallery_video', EntityType::class, array(
                'class' => CategoryGallery::class,
                'multiple' => true,
                'expanded' => true,
                'choice_label' => 'gallery_video',
            ))
        ;




  • Partager sur Facebook
  • Partager sur Twitter
26 janvier 2020 à 16:11:17

Salut

Tu ne sembles pas avoir compris à quoi servait le paramètre qu'on t'a demandé d'ajouter. Il permet entre autres de spécifier le nom de la propriété (donc pas de la colonne de base de données) scalaire (donc pas une relation) de l'entité utilisée dans la liste, afin d'utiliser cette propriété comme représentation textuelle. Comme tu veux le nom de l'entité CategoryGallery et que ce nom est enregistré dans la propriété $name de l'entité, je pense que tu as désormais tout ce qu'il te faut pour comprendre quoi mettre comme valeur et pourquoi.

  • Partager sur Facebook
  • Partager sur Twitter