bjr , j'ai voulu crée une option et j'ai cette errreur:
Cannot assign Doctrine\ORM\PersistentCollection to property App\Entity\Property::$options of type Doctrine\Common\Collections\ArrayCollection
et j'ai cette erreur même lorsque je veux navigué sur le reste du site sauf la page create & index du crud option.
quelqu'un peu t il m aidé svp merci d'avance!

2 réponses


Glioburd
Réponse acceptée

He ben comme j'ai dis dans mon premier post : essaie en typant la propriété $options avec Collection :

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

class Property
{
    /**
     * @ORM\ManyToMany(targetEntity="Option::class", inversedBy="properties")
     */
    private Collection $options;

    // ...

    public function __construct()
    {
        $this->options = new ArrayCollection();
        // ...
    }
}

Salut, si tu veux de l'aide, poste le code de tes entités (Property et Option).

Tu as du faire un erreur au niveau des annotations pour les relations, ou bien au niveau du typage de la propriété $options (type Collection au lieu d'actuellement ArrayCollection)

bonjour,
voici le code Property:
`<?php

namespace App\Entity;

use App\Repository\PropertyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Cocur\Slugify\Slugify;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**

  • @ORM\Entity(repositoryClass=PropertyRepository::class)
  • @UniqueEntity("title")
    */
    class Property
    {
    const HEAT = [
    0 => 'Electrique',
    1 => 'Gaz'
    ];
    /**

    • @ORM\Id
    • @ORM\GeneratedValue
    • @ORM\Column(type="integer")
      */
      private ?int $id;

    /**

    • @Assert\Length(min="5", max="255")
    • @ORM\Column(type="string", length=255)
      */
      private ?string $title;

    /**

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

    /**

    • @ORM\Column(type="integer")
      */
      private ?int $surface;

    /**

    • @ORM\Column(type="integer")
    • @Assert\Range(min=10, max=400)
      */
      private ?int $rooms;

    /**

    • @ORM\Column(type="integer")
      */
      private ?int $bedrooms;

    /**

    • @ORM\Column(type="integer")
      */
      private ?int $floor;

    /**

    • @ORM\Column(type="integer")
      */
      private ?int $price;

    /**

    • @ORM\Column(type="integer")
      */
      private ?int $heat;

    /**

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

    /**

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

    /**

    • @Assert\Regex("/^[0-9]{5}$/")
    • @ORM\Column(type="string", length=255)
      */
      private ?string $postal_code;

    /**

    • @ORM\Column(type="boolean", options={"default": false})
      */
      private bool $sold = false;

    /**

    • @ORM\Column(type="datetime")
      */
      private \DateTime $created_at;

    /**

    • @ORM\ManyToMany(targetEntity=Option::class, inversedBy="properties")
      */
      private ArrayCollection $options;

    public function __construct(){
    $this->created_at = new \DateTime();
    $this->options = 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 getSlug(): string
    {
    return (new Slugify())->slugify($this->title);
    }

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

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

    return $this;

    }

    public function getSurface(): ?int
    {
    return $this->surface;
    }

    public function setSurface(int $surface): self
    {
    $this->surface = $surface;

    return $this;

    }

    public function getRooms(): ?int
    {
    return $this->rooms;
    }

    public function setRooms(int $rooms): self
    {
    $this->rooms = $rooms;

    return $this;

    }

    public function getBedrooms(): ?int
    {
    return $this->bedrooms;
    }

    public function setBedrooms(int $bedrooms): self
    {
    $this->bedrooms = $bedrooms;

    return $this;

    }

    public function getFloor(): ?int
    {
    return $this->floor;
    }

    public function setFloor(int $floor): self
    {
    $this->floor = $floor;

    return $this;

    }

    public function getPrice(): ?int
    {
    return $this->price;
    }

    public function getFormattedPrice():string
    {
    return number_format($this->price, 0,'', ' ');
    }

    public function setPrice(int $price): self
    {
    $this->price = $price;

    return $this;

    }

    public function getHeat(): ?int
    {
    return $this->heat;
    }

    public function setHeat(int $heat): self
    {
    $this->heat = $heat;

    return $this;

    }

    public function getHeatType(): string
    {
    return self::HEAT[$this->heat];
    }

    public function getCity(): ?string
    {
    return $this->city;
    }

    public function setCity(string $city): self
    {
    $this->city = $city;

    return $this;

    }

    public function getAddress(): ?string
    {
    return $this->address;
    }

    public function setAddress(string $address): self
    {
    $this->address = $address;

    return $this;

    }

    public function getPostalCode(): ?string
    {
    return $this->postal_code;
    }

    public function setPostalCode(string $postal_code): self
    {
    $this->postal_code = $postal_code;

    return $this;

    }

    public function getSold(): ?bool
    {
    return $this->sold;
    }

    public function setSold(bool $sold): self
    {
    $this->sold = $sold;

    return $this;

    }

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

    public function setCreatedAt(\DateTime $created_at): self
    {
    $this->created_at = $created_at;

    return $this;

    }

    /**

    • @return Collection|Option[]
      */
      public function getOptions(): Collection
      {
      return $this->options;
      }

    public function addOption(Option $option): self
    {
    if (!$this->options->contains($option)) {
    $this->options[] = $option;
    $option->addProperty($this);
    }

    return $this;

    }

    public function removeOption(Option $option): self
    {
    if ($this->options->removeElement($option)) {
    $option->removeProperty($this);
    }

    return $this;

    }
    }
    et celui Option: <?php

namespace App\Entity;

use App\Repository\OptionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;

/**

  • @ORM\Entity(repositoryClass=OptionRepository::class)
  • @ORM\Table(name="option")
    */
    class Option
    {
    /**

    • @ORM\Id
    • @ORM\GeneratedValue
    • @ORM\Column(type="integer")
      */
      private ?int $id;

    /**

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

    /**

    • @ORM\ManyToMany(targetEntity=Property::class, mappedBy="options")
      */
      private ArrayCollection $properties;
    [Pure] public function __construct()

    {
    $this->properties = 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;

    }

    /**

    • @return Collection
      */
      public function getProperties(): Collection
      {
      return $this->properties;
      }

    public function addProperty(Property $property): self
    {
    if (!$this->properties->contains($property)) {
    $this->properties[] = $property;
    }

    return $this;

    }

    public function removeProperty(Property $property): self
    {
    $this->properties->removeElement($property);

    return $this;

    }
    }
    `
    merci!