无法在doctrine上删除具有继承的实体

问题描述 投票:0回答:1

有了这两个实体

Entity\AbstractSurveyAnswer:
    type: entity
    table: survey_answers
    inheritanceType: JOINED
    discriminatorColumn:
        name: type_class
        type: string
    discriminatorMap:
        database: SurveyDatabaseAnswer
        client_text: SurveyClientTextAnswer
        client_number: SurveyClientNumberAnswer
        client_date: SurveyClientDateAnswer

    id:
        id:
            type: integer
            generator:
                strategy: AUTO

    manyToOne:
        client:
            targetEntity: Entity\Client
            joinColumn:
                name: id_client
                nullable: false
                referenceColumnName: id

        survey:
            targetEntity: Entity\AbstractSurvey
            joinColumn:
                name: id_survey
                nullable: false
                referenceColumnName: id

Entity\AbstractSurveyAnswer:
    type: entity
    table: survey_answers
    inheritanceType: JOINED
    discriminatorColumn:
        name: type_class
        type: string
    discriminatorMap:
        database: SurveyDatabaseAnswer
        client_text: SurveyClientTextAnswer
        client_number: SurveyClientNumberAnswer
        client_date: SurveyClientDateAnswer

    id:
        id:
            type: integer
            generator:
                strategy: AUTO

    manyToOne:
        client:
            targetEntity: Entity\Client
            joinColumn:
                name: id_client
                nullable: false
                referenceColumnName: id

        survey:
            targetEntity: Entity\AbstractSurvey
            joinColumn:
                name: id_survey
                nullable: false
                referenceColumnName: id

我尝试使用此代码删除实体

$to_delete_answer = new SurveyDatabaseAnswer();
$to_delete_answer->setId($answer);
$storage->remove($to_delete_answer);//calls EntityManagger::remove
$storage->flush();

但是我收到以下错误消息

Detached entity \Entity\\SurveyDatabaseAnswer@00000000204763e700007fdd1d66d304 cannot be removed

我不知道删除这个实体有什么问题,而不是尝试删除另一个表上引用的ant表。这是因为继承类型的问题吗?我是教条上的菜鸟,我看不出问题

php doctrine
1个回答
0
投票

我认为你的问题是你的实体$ to_delete_answer没有“合并”,所以你的EntityManager无法处理它。尝试在删除实体之前添加$storage->merge($to_delete_answer);

$to_delete_answer = new SurveyDatabaseAnswer();
$to_delete_answer->setId($answer);
$storage->merge($to_delete_answer);
$storage->remove($to_delete_answer);//calls EntityManagger::remove
$storage->flush();

见:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html#merging-entities

© www.soinside.com 2019 - 2024. All rights reserved.