Warning: Illegal offset type in isset or empty In ArrayCollection.php line 133 #6212

Closed
opened 2026-01-22 15:29:00 +01:00 by admin · 1 comment
Owner

Originally created by @liveisdestiny on GitHub (Apr 2, 2019).

Originally assigned to: @Ocramius on GitHub.

Bug Report

Q A
BC Break no
Version ^2.6.3

Summary

Removing an Entity from Array Collection throws the error. The collection contains the element and it is checked with contains method before without problems

Current behavior

In ArrayCollection.php line 133:
                                                  
  [ErrorException]                                
  Warning: Illegal offset type in isset or empty  
                                                  

Exception trace:
 () at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php:133
 Doctrine\Common\Collections\ArrayCollection->remove() at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:83
 Doctrine\Common\Collections\AbstractLazyCollection->remove() at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:346
 Doctrine\ORM\PersistentCollection->remove() at /media/destiny/Data/Files/develop/symfony/src/Core/Acl/Entity/Role.php:235

// trace to command here

How to reproduce

Create a Symfony console command (CC) and an entity (Roles). Now try to remove another Entity (User) form the List:

CC:

...
$role->removeUser($users[$dataset[1]]); // $users[$dataset[1]) contains a valid and saved User Entity
...

Roles:


/**
     * @ORM\ManyToMany(targetEntity="XXXX", mappedBy="roles", cascade={"all"},
     *                                                                      indexBy="id")
     *
     * @var User[]|Collection
     */
    private $users; // defined as ArrayCollection in constructor. Contains the requested User


    public function removeUser(User $user): void {
        if ($this->users->contains($user)) { // works fine
            $this->users->remove($user);  // throws exception
            $user->removeRole($this);
        }
    }

ArrayCollection:

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

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

        return $removed;
    }

The problem caused on $this->elements[$key]

Expected behavior

It should remove the Entity (User) from the Collection but it throws an exception. Adding it works fine

Originally created by @liveisdestiny on GitHub (Apr 2, 2019). Originally assigned to: @Ocramius on GitHub. ### Bug Report | Q | A |------------ | ------ | BC Break | no | Version | ^2.6.3 #### Summary Removing an Entity from Array Collection throws the error. The collection contains the element and it is checked with contains method before without problems #### Current behavior ``` In ArrayCollection.php line 133: [ErrorException] Warning: Illegal offset type in isset or empty Exception trace: () at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php:133 Doctrine\Common\Collections\ArrayCollection->remove() at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:83 Doctrine\Common\Collections\AbstractLazyCollection->remove() at /media/destiny/Data/Files/develop/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php:346 Doctrine\ORM\PersistentCollection->remove() at /media/destiny/Data/Files/develop/symfony/src/Core/Acl/Entity/Role.php:235 // trace to command here ``` #### How to reproduce Create a Symfony console command (CC) and an entity (Roles). Now try to remove another Entity (User) form the List: CC: ``` ... $role->removeUser($users[$dataset[1]]); // $users[$dataset[1]) contains a valid and saved User Entity ... ``` Roles: ``` /** * @ORM\ManyToMany(targetEntity="XXXX", mappedBy="roles", cascade={"all"}, * indexBy="id") * * @var User[]|Collection */ private $users; // defined as ArrayCollection in constructor. Contains the requested User public function removeUser(User $user): void { if ($this->users->contains($user)) { // works fine $this->users->remove($user); // throws exception $user->removeRole($this); } } ``` ArrayCollection: ``` public function remove($key) { if (! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) { // this is line 133 return null; } $removed = $this->elements[$key]; unset($this->elements[$key]); return $removed; } ``` The problem caused on `$this->elements[$key]` #### Expected behavior It should remove the Entity (User) from the Collection but it throws an exception. Adding it works fine
admin added the BugInvalid labels 2026-01-22 15:29:00 +01:00
admin closed this issue 2026-01-22 15:29:00 +01:00
Author
Owner

@Ocramius commented on GitHub (Apr 2, 2019):

You are looking for Collection#removeElement()

@Ocramius commented on GitHub (Apr 2, 2019): You are looking for [`Collection#removeElement()`](https://github.com/doctrine/collections/blob/fcf1b009acbf4aca5e48c1cc311bc4444f88bdf3/lib/Doctrine/Common/Collections/Collection.php#L83-L92)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6212