Doctrine - delete self-many-to-many

38 Views Asked by At

Custom list of items. Each item can contain relation to more items (self-many-to-many):

#[ORM\Entity]
class CustomListItem
{
    use IdentifierAware;

    #[ORM\Column]
    protected string $value;

    #[ORM\ManyToOne(inversedBy: 'items')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    protected CustomList $customList;

    /**
     * @var Collection<int, CustomListItem>
     */
    #[ORM\ManyToMany(targetEntity: CustomListItem::class, inversedBy: 'children')]
    protected Collection $parents;

    /**
     * @var Collection<int, CustomListItem>
     */
    #[ORM\ManyToMany(targetEntity: CustomListItem::class, mappedBy: 'parents')]
    protected Collection $children;

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

    ...
}

I need clear all parents for all items by a specific custom list. Like this:

foreach ($customList->getItems() as $item) {
    $item->getParents()->clear();
}

The problem is performance for thousands of items. I have no idea how to write a DQL query. Can you help me?

0

There are 0 best solutions below