Removing an entity causes InvalidArgumentException #5025

Open
opened 2026-01-22 14:56:52 +01:00 by admin · 0 comments
Owner

Originally created by @olegkrivtsov on GitHub (Feb 26, 2016).

Hi, I'm having a 'EmailFolder' entity and trying to delete all mail folders like this:

$folders = $entityManager->getRepository('Application\Entity\EmailFolder')
                ->findBy(array('user'=>$user));
foreach ($folders as $emailFolder) {            
            $entityManager->remove($emailFolder);
            $entityManager->flush();
}    

This raises an InvalidArgumentException exception:

 Doctrine\ORM\ORMInvalidArgumentException
 A new entity was found through the relationship 'Application\Entity\EmailFolder#parentFolder' that was not configured to cascade persist operations for entity: Application\Entity\EmailFolder@0000000068c39bba0000000018303d0f. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist  this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Application\Entity\EmailFolder#__toString()' to get a clue.

But, when I delete the entities as follows, no exception is raised:

foreach ($folders as $emailFolder) {            
     $entityManager->remove($emailFolder);    
}        
$entityManager->flush();

What's the difference? Why in the first case there is an exception, but in the second one there is no exception?

Here is the code of the entity:

<?php
namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Application\Entity\EmailAddress;

/**
 * This class represents an email folder.
 * @ORM\Entity()
 * @ORM\Table(name="email_folder")
 */
class EmailFolder
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id")
     * @ORM\GeneratedValue
     */
    protected $id;

    /** 
     * @ORM\Column(name="folder_id")  
     */
    protected $folderId;

    /** 
     * @ORM\Column(name="name")  
     */
    protected $name;    

    /** 
     * @ORM\Column(name="delta_link")  
     */
    protected $deltaLink;

    /** 
     * @ORM\Column(name="last_synchronized")  
     */
    protected $lastSynchronized;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\User", inversedBy="emailFolders")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Application\Entity\EmailFolder", inversedBy="childFolders")
     * @ORM\JoinColumn(name="parent_folder_id", referencedColumnName="id")
     */
    protected $parentFolder;

    /**
     * Constructor.
     */
    public function __construct() 
    {
    }

    /**
     * Returns ID.
     * @return integer
     */
    public function getId() 
    {
        return $this->id;
    }

    public function getFolderId() 
    {
        return $this->folderId;
    }

    public function setFolderId($folderId) 
    {
        $this->folderId = $folderId;
    }

    public function getParentFolder() 
    {
        return $this->parentFolder;
    }

    public function setParentFolder($parentFolder) 
    {
        $this->parentFolder = $parentFolder;
    }

    public function getName() 
    {
        return $this->name;
    }

    public function setName($name) 
    {
        $this->name = $name;
    }

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

    public function setUser($user) 
    {
        $this->user = $user;
    }

    public function getDeltaLink() 
    {
        return $this->deltaLink;
    }

    public function setDeltaLink($deltaLink) 
    {
        $this->deltaLink = $deltaLink;
    }

    public function getLastSynchronized() 
    {
        return $this->lastSynchronized;
    }

    public function setLastSynchronized($lastSynchronized)
    {
        $this->lastSynchronized = $lastSynchronized;
    }
}
Originally created by @olegkrivtsov on GitHub (Feb 26, 2016). Hi, I'm having a 'EmailFolder' entity and trying to delete all mail folders like this: ``` $folders = $entityManager->getRepository('Application\Entity\EmailFolder') ->findBy(array('user'=>$user)); foreach ($folders as $emailFolder) { $entityManager->remove($emailFolder); $entityManager->flush(); } ``` This raises an `InvalidArgumentException` exception: ``` Doctrine\ORM\ORMInvalidArgumentException A new entity was found through the relationship 'Application\Entity\EmailFolder#parentFolder' that was not configured to cascade persist operations for entity: Application\Entity\EmailFolder@0000000068c39bba0000000018303d0f. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Application\Entity\EmailFolder#__toString()' to get a clue. ``` But, when I delete the entities as follows, no exception is raised: ``` foreach ($folders as $emailFolder) { $entityManager->remove($emailFolder); } $entityManager->flush(); ``` What's the difference? Why in the first case there is an exception, but in the second one there is no exception? Here is the code of the entity: ``` <?php namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Application\Entity\EmailAddress; /** * This class represents an email folder. * @ORM\Entity() * @ORM\Table(name="email_folder") */ class EmailFolder { /** * @ORM\Id * @ORM\Column(name="id") * @ORM\GeneratedValue */ protected $id; /** * @ORM\Column(name="folder_id") */ protected $folderId; /** * @ORM\Column(name="name") */ protected $name; /** * @ORM\Column(name="delta_link") */ protected $deltaLink; /** * @ORM\Column(name="last_synchronized") */ protected $lastSynchronized; /** * @ORM\ManyToOne(targetEntity="Application\Entity\User", inversedBy="emailFolders") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $user; /** * @ORM\ManyToOne(targetEntity="Application\Entity\EmailFolder", inversedBy="childFolders") * @ORM\JoinColumn(name="parent_folder_id", referencedColumnName="id") */ protected $parentFolder; /** * Constructor. */ public function __construct() { } /** * Returns ID. * @return integer */ public function getId() { return $this->id; } public function getFolderId() { return $this->folderId; } public function setFolderId($folderId) { $this->folderId = $folderId; } public function getParentFolder() { return $this->parentFolder; } public function setParentFolder($parentFolder) { $this->parentFolder = $parentFolder; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getUser() { return $this->user; } public function setUser($user) { $this->user = $user; } public function getDeltaLink() { return $this->deltaLink; } public function setDeltaLink($deltaLink) { $this->deltaLink = $deltaLink; } public function getLastSynchronized() { return $this->lastSynchronized; } public function setLastSynchronized($lastSynchronized) { $this->lastSynchronized = $lastSynchronized; } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5025