Как получить доступ к свойству, имеющему массив типа в отношениях «многие ко многим» с промежуточной таблицей - PullRequest
0 голосов
/ 09 мая 2019

вот ошибка, которую возвращает symfony4: не удалось определить тип доступа для свойства "artist" в классе "App \ Entity \ Event": свойство "artist" в классе "App \ Entity \ Event" можно определить с помощьюметоды «addArtist ()», «removeArtist ()», но новое значение должно быть массивом или экземпляром \ Traversable, заданным «App \ Entity \ Artist».`

<?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\EventRepository")
 */
class Event
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $title;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $description;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $begin_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $end_at;

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

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

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

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $counter;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Place", inversedBy="events")
     */
    private $place;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Artist", inversedBy="events")
     */
    private $artist;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): self
    {
        $this->title = $title;

        return $this;
    }

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

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

        return $this;
    }

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

    public function setBeginAt(?\DateTimeInterface $begin_at): self
    {
        $this->begin_at = $begin_at;

        return $this;
    }

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

    public function setEndAt(?\DateTimeInterface $end_at): self
    {
        $this->end_at = $end_at;

        return $this;
    }

    public function getWeb(): ?string
    {
        return $this->web;
    }

    public function setWeb(?string $web): self
    {
        $this->web = $web;

        return $this;
    }

    public function getFacebook(): ?string
    {
        return $this->facebook;
    }

    public function setFacebook(?string $facebook): self
    {
        $this->facebook = $facebook;

        return $this;
    }

    public function getInstagram(): ?string
    {
        return $this->instagram;
    }

    public function setInstagram(?string $instagram): self
    {
        $this->instagram = $instagram;

        return $this;
    }

    public function getCounter(): ?int
    {
        return $this->counter;
    }

    public function setCounter(?int $counter): self
    {
        $this->counter = $counter;

        return $this;
    }

    public function getPlace(): ?Place
    {
        return $this->place;
    }

    public function setPlace(?Place $place): self
    {
        $this->place = $place;

        return $this;
    }

    /**
     * @return Collection|Artist[]
     */
    public function getArtist(): Collection
    {
        return $this->artist;
    }
//-------------mi solucion---
    //public function setArtist(?string $artist): self
    //{
    //    $this->artist = $artist;

    //    return $this;
    //}
//---------------------
    public function addArtist(Artist $artist): self
    {
        if (!$this->artist->contains($artist)) {
            $this->artist[] = $artist;
        }

        return $this;
    }

    public function removeArtist(Artist $artist): self
    {
        if ($this->artist->contains($artist)) {
            $this->artist->removeElement($artist);
        }

        return $this;
    }
}

// class Artist

<?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\ArtistRepository")
 */
class Artist
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $description;

    /**
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    private $phone_nb;

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="artists")
     */
    private $category;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Event", mappedBy="artist")
     * @ORM\JoinTable(name="event_artist")
     */
    private $events;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", mappedBy="artiste", cascade={"persist", "remove"})
     */
    private $user;

    public function __construct()
    {
        $this->events = 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 getDescription(): ?string
    {
        return $this->description;
    }

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

        return $this;
    }

    public function getPhoneNb(): ?string
    {
        return $this->phone_nb;
    }

    public function setPhoneNb(?string $phone_nb): self
    {
        $this->phone_nb = $phone_nb;

        return $this;
    }

    public function getWeb(): ?string
    {
        return $this->web;
    }

    public function setWeb(?string $web): self
    {
        $this->web = $web;

        return $this;
    }

    public function getFacebook(): ?string
    {
        return $this->facebook;
    }

    public function setFacebook(?string $facebook): self
    {
        $this->facebook = $facebook;

        return $this;
    }

    public function getInstagram(): ?string
    {
        return $this->instagram;
    }

    public function setInstagram(?string $instagram): self
    {
        $this->instagram = $instagram;

        return $this;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): self
    {
        $this->category = $category;

        return $this;
    }

    /**
     * @return Collection|Event[]
     */
    public function getEvent(): Collection
    {
        return $this->events;
    }

    public function setEvent(?Event $event): self
    {
        $this->events = $event;
        return $this;
    }

    public function addEvent(Event $event): self
    {
        if (!$this->events->contains($event)) {
            $this->events[] = $event;
            $event->addArtist($this);
        }

        return $this;
    }

    public function removeEvent(Event $event): self
    {
        if ($this->events->contains($event)) {
            $this->events->removeElement($event);
            $event->removeArtist($this);
        }

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        // set (or unset) the owning side of the relation if necessary
        $newArtiste = $user === null ? null : $this;
        if ($newArtiste !== $user->getArtiste()) {
            $user->setArtiste($newArtiste);
        }

        return $this;
    }
}

//EventType

class EventType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, ['label'=>'Titre'])
            ->add('description', TextType::class, ['label'=>'Description'])
            ->add('begin_at', DateTimeType::class, ['label'=>'Début'])
            ->add('end_at', DateTimeType::class, ['label'=>'Fin'])
            ->add('web')
            ->add('facebook')
            ->add('instagram')
            //->add('counter')
            ->add('place', EntityType::class, ['class'=>'App:Place', 'choices'=>$options['place'], 'choice_label'=>'title', 'label'=>'Nom du lieu'])
            ->add('artist', EntityType::class, ['class'=>'App:Artist', 'choices'=>$options['artist'], 'choice_label'=>'name', 'label'=>'Artiste'])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Event::class,
            'place' => null,
            'artist' =>null
        ]);
    }
}

// Event Conrtroller

class EventController extends AbstractController
{
    /**
     * @Route("/event/new", name="new_event")
     * @Route("/event/{id}/edit", name="edit_event")
     */
    public function eventForm(event $event = null, Request $request, ObjectManager $manager){
        if(!$event){
            $event = new Event();
        }

        $form = $this->createForm(EventType::class, $event);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()) {
            $manager->persist($event);
            $manager->flush();
            return $this->redirectToRoute ('show_event', ['id'=>$event->getId()]);
        }
        return $this->render('event/edit_event.html.twig',[
            'formEvent' => $form->createView(),
            'editMode'=> $event->getId() !==null
        ]);
    }

    /**
     * @Route("/event/{id}", name="show_event")
     */
    public function showEvent($id) {
        $repo = $this->getDoctrine()->getRepository(Event::class);
        $event = $repo->find($id);
        return $this->render('event/show_event.html.twig',
        [
            'controller_name' => 'EventController',
            'event' => $event
        ]);
    }

    /**
     * @Route("/", name="index_event")
     */
    public function index(EventRepository $repo) {
        $events = $repo->findAll();
        return $this->render('event/home_event.html.twig', [
            'controller_name' => 'EventController',
            'events' => $events
        ]);
    }
}

`

...