学说明确和嵌入实体

时间:2017-07-03 14:39:46

标签: symfony doctrine-orm

我不明白为什么我必须清除嵌入式实体,你能解释一下吗?

我处在一个很大的处理中,所以我只粘贴了算法的逻辑。我知道我不必每次都要冲洗(我在symfony doc中阅读批处理)

使用此代码:

foreach ($buildings as $building) {
    $appartments = $this->em->getRepository('Appartment')->findByBuilding($building['id']);

    /**
     * @var $appartment Appartment
     */
    foreach ($appartments as $appartment) {


        $log = new Log();
        $log->setAppartment($appartment)
            ->setLabel('test');

        $this->em->persist($log);
        $this->em->flush();
        $this->em->clear(Log::class);

    }
    $this->em->clear(Appartment::class);
}

我有这个错误:

A new entity was found through the relationship 'AppBundle\Entity\Owner#appartment' that was not configured to cascade persist

但是当我这样做时:

$this->em->clear(Appartment::class);
$this->em->clear(Owner::class);

它有效

这是一个例子,但在我的项目中,我有很多相关的实体。我是否必须检查所有关系以清除它们?

你能解释一下这个问题吗?

感谢

编辑:这里是实体(这篇文章是一个例子,所以我可能会犯错误)

公寓:

AppBundle\Entity\Appartment:
    type: entity
    repositoryClass: AppBundle\Repository\AppartmentRepository
    table: appartment
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        ....
    oneToOne:
        owner:
            targetEntity: AppartmentOwner
            mappedBy: appart

所有者:

AppBundle\Entity\AppartmentOwner:
    type: entity
    table: appart_owner
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        ....
    oneToOne:
        appart:
            targetEntity: Appartment
            joinColumn:
                name: appart_id
                referencedColumnName: id
                nullable: false

0 个答案:

没有答案