0
votes

I've been trying to get a hold of a solution for this for some time now, but failed: When I try to persist a new entity in Doctrine 2.3 and flush afterwards, I receive:

CRITICAL: Doctrine\ORM\ORMInvalidArgumentException: A new entity was found through the relationship 'Task#parentTask' that was not configured to cascade persist operations for entity

I have a self-referencing entity Task that looks -- in a condensed view --like this:

class Task
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
     private $id;

     /** 
     * @ManyToOne(targetEntity="tasks\classes\Task", inversedBy="childTasks", fetch="LAZY")
     * @JoinColumn(name="parent", referencedColumnName="id")
     */
    private $parentTask;

    /** 
     * @OneToMany(targetEntity="tasks\classes\Task", mappedBy="parentTask", fetch="LAZY")
     */
    private $childTasks;
}

And based on this task, I'm Now I'm fetching a task using a query built in QueryBuilder:

   function getTasksCreatedByUser($user) 
   {
        $em = $this->db->getEntityManager();
        $qb = $em->createQueryBuilder();
        $query = $qb->select("t")
            ->from("tasks\classes\Task", "t")
            ->where($qb->expr()->andX(
                "t.creator = :creator"
                // plus more conditions here
            ))  
            ->setParameter("creator", $user)
            ->orderBy("t.id", "DESC")
            ->getQuery()
            ;   
        return $query->getResult();
    }   

For each of these tasks, I create a new task referencing them as $parentTask (code shortened):

foreach($tasks as $task) {
    $newTask = new \tasks\classes\Task();
    $newTask->setParentTask($task);
    $db->persist($newTask);
}
class DB
{
    public function persist($entity)
    {
        $this->entityManager->persist($object);
        $this->entityManager->flush();

    }
}

In other parts of my application, the same pattern works fine, and I cannot find what the difference is.

Can any of you help me understand why the exception is thrown? I read through a dozen of other threads referencing the same exception, and usually it was the case that there was somehow a relationship between two objects, both not persisted so far; one would be persisted, the other not, and that would throw the exception. I cannot see that happening in my case though.

Any help is appreciated!

2

2 Answers

1
votes

Try this annotation in your Task class.

/** 
 * @OneToMany(targetEntity="tasks\classes\Task", mappedBy="parentTask", cascade={"persist", "remove"}, fetch="LAZY")
 */
private $childTasks;
0
votes

I found the solution to my problem. I'll explain it here in the hope that this will help someone else one day.

I have two parts of my application: One part that is the REST backend for my web interface; the second part that offers CLI operations to execute longer calculations. For both, I instantiate a Doctrine EntityManager using the same code. I use this EntityManager#1 for multiple model and helper classes that access the database.

However, due to me forgetting about fact that I already had an instance, I created a second entityManager in my CLI code (EntityManager#2) that handled CLI-specific DB access. This lead to the situation: getTasksCreatedByUser() worked with EntityManager#1, but the portion in foreach($tasks as $task) with EntityManager#2.

When I took away the second instantiation, everything just worked like a charm.