3

I've a ManyToMany relation in Listing entity:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ListingRepository")
 * @ORM\Table(name="listings")
 */
class Listing extends MainEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\AttributeDescription", inversedBy="listings", cascade={"persist", "remove"})
     * @JoinTable(name="listing_attriubute_descriptions",
     *      joinColumns={@JoinColumn(name="listing_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="attribute_description_id", referencedColumnName="id")}
     *      )
     */
    public $attributeDescriptions;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->attributeDescriptions = new ArrayCollection();
    }

    public function removeAttributeDescription(AttributeDescription $attributeDescription)
    {
        if(!$this->attributeDescriptions->contains($attributeDescription))
        {
            return;
        }
        $this->attributeDescriptions->remove($attributeDescription);

        return $this;
    }
}

Somewhere in the ListingService I'm trying to remove AttributeDescription from the Listing entity like this:

$listing->removeAttributeDescription($toRemoveAttributeDescription);

But got an error: Warning: Illegal offset type in isset or empty

Using xdebug I've landed to remove() method in ArrayCollection:

public function remove($key)
{
    if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) {
        return null;
    }

    $removed = $this->elements[$key];
    unset($this->elements[$key]);

    return $removed;
}

And found out that the problem comes from isset($this->elements[$key]). Here is the screenshot from xdebug:

enter image description here

As you can see the $key contains an AttributeDescription, and the $this->elements is an array of AttributeDescriptions.

I really don't get whether I'm doing something wrong or is this a doctrine bug?

I'm using: Symfony 3.3.13 with php 7.1.1

Doctrine:

"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/doctrine-migrations-bundle": "^1.2",
"doctrine/orm": "^2.5"
rvaliev
  • 1,051
  • 2
  • 12
  • 31

1 Answers1

12

The solution: I was using wrong method. Instead of remove() I should use removeElement()

rvaliev
  • 1,051
  • 2
  • 12
  • 31