doulilou8560 2015-10-26 14:35
浏览 54
已采纳

学说:通过这种关系找到了一个新实体......当试图冲洗时

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条回答 默认 最新

  • doucheng2053 2015-10-30 09:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办