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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效