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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化