为什么Doctrine会尝试通过关系插入新实体?

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

我有以下教义实体,其中EntityA是拥有方:

<?php

class EntityA
{
    /**
     * @ORM\ManyToOne(targetEntity="EntityB" , inversedBy="propertyA")
     */
    private $propertyB;

    // ....

    public function setEntityB(EntityB $entBRef)
    {
        $this->propertyB = $entBRef;
    }
}

class EntityB
{
    /**
     * @ORM\OneToMany(targetEntity="EntityA", mappedBy="propertyB")
     */
    private $propertyA;
    // ....
}

我也有一个RepositoryB类:

<?php 

class RepositoryB extends \Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository
{
    public function __construct(\Doctrine\Common\Persistence\ManagerRegistry $registry)
    {
        parent::__construct($registry, EntityB::class);
    }
}

然后在我的课堂上,我试图保留EntityA对象:

<?php

class SomeService
{
    /** @var RepositoryB */
    private $repositoryB;

    /** @var EntityManagerInterface */
    private $entityManager;

    public function __construct(RepositoryB $repositoryB, EntityManagerInterface $entityManager)
    {
        $this->repositoryB = $repositoryB;
        $this->entityManager = $entityManager;
    }

    public function testSomething()
    {
        $entBRef = $this->repositoryB->find(1);

        $newEnt = (new EntityA());
        $newEnt->setEntityB($entBRef);
        $this->entityManager->persist($newEnt);
        $this->flush();
    }
}

但是我遇到以下错误:

通过关系'EntityA#propertyB找到了一个新实体没有配置为级联实体的持久化操作:实体B。解决此问题的方法:显式调用此未知实体上的EntityManager#persist()或配置级联

这是我的doctrine.yaml文件的外观:

parameters:
    env(DATABASE_URL_WRITE): ''

services:
    gedmo.listener.timestampable:
        class: Org\CompBundle\Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]

    gedmo.listener.loggable:
        class: Gedmo\Loggable\LoggableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ "@annotation_reader" ] ]

    gedmo.listener.deleteable:
        class: Gedmo\SoftDeleteable\SoftDeleteableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ '@annotation_reader' ] ]

doctrine:
    dbal:
        default_connection: default
        types:
            microseconds: Org\CompBundle\DBAL\Types\DateTimeMicrosecondsType
        connections:
            read_only:
                url: '%env(resolve:DATABASE_URL_READ)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
            default:
                url: '%env(resolve:DATABASE_URL_WRITE)%'
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        default_entity_manager: default
        entity_managers:
            filters:
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true
            read_only:
                connection: read_only
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    gedmo_loggable:
                        type: annotation
                        prefix: Gedmo\Loggable\Entity
                        dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
                        alias: GedmoLoggable
                        is_bundle: false
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/vendor/org/comp-bundle/Entity'
                        prefix: 'Org\CompBundle\Entity'
                        alias: Drm
                dql:
                    datetime_functions:
                        timetosec: DoctrineExtensions\Query\Mysql\TimeToSec
                        timediff: DoctrineExtensions\Query\Mysql\TimeDiff
                        now: DoctrineExtensions\Query\Mysql\Now
                    numeric_functions:
                        rand: DoctrineExtensions\Query\Mysql\Rand
            default:
                connection: default
                filters:
                    softdeleteable:
                        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                        enabled: true
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                auto_mapping: true
                mappings:
                    gedmo_loggable:
                        type: annotation
                        prefix: Gedmo\Loggable\Entity
                        dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
                        alias: GedmoLoggable
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/vendor/org/comp-bundle/Entity'
                        prefix: 'Org\CompBundle\Entity'
                        alias: Drm
                dql:
                    datetime_functions:
                        timetosec: DoctrineExtensions\Query\Mysql\TimeToSec
                        timediff: DoctrineExtensions\Query\Mysql\TimeDiff
                        now: DoctrineExtensions\Query\Mysql\Now
                    numeric_functions:
                        rand: DoctrineExtensions\Query\Mysql\Rand

最后但并非最不重要的一点是,我在org\compbundle\Resources\services.yaml进行了以下设置:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false
        bind:
            $logger: '@Psr\Log\LoggerInterface'

    Org\CompBundle\Repository\:
        resource: '../../Repository/{Case,Main}'
        exclude: ['../../Repository/**/*Interface.php']
        tags: ['doctrine.repository_service']

    Org\CompBundle\Interfaces\Queues\QueueRepositoryInterface:
        class: Org\CompBundle\Repository\DrmCase\QueueRepository

    Org\CompBundle\Interfaces\Cases\CasesRepositoryInterface:
        class: Org\CompBundle\Repository\DrmCase\CasesRepository

    Doctrine\Common\Persistence\ManagerRegistry: '@doctrine'

到目前为止我做了什么?

  1. 在此处阅读了很多导致相同解决方案的文章,在我所拥有的一方增加了cascade={"persist"},我必须说这没有用。
  2. 阅读Doctrine文档,寻找有关我的实体定义的错误,但到目前为止,一切对我来说都不错。

我确实发现了this很有帮助,但是我一直在问自己为什么Doctrine会仅仅因为引用而试图插入并存在实体?有什么办法可以使它正常工作?

我不希望EntityB保持不变或更新,它已经存在。我确实希望新的EntityA对EntityB中的现有记录具有FK。

symfony doctrine-orm doctrine symfony4
1个回答
0
投票

[在周末期间,一位工作同事非常努力地找出导致问题的原因,并在大量小时调试了Doctrine和UoW的工作方式后,他发现了问题所在:Doctrine和存储库模式如何工作。让我解释一下。

如果仔细检查并深入研究Doctrine,您将注意到以下代码如何导致两个不同的实体管理器:

public function __construct(RepositoryB $repositoryB, EntityManagerInterface $entityManager)
{
    $this->repositoryB = $repositoryB; // spin his own EM
    $this->entityManager = $entityManager; // this is a new EM object
}

所以:

$entBRef = $this->repositoryB->find(1);

正在通过另一个EM获取,并且由于我使用$this->entityManager来持久化/刷新最近创建的entityA对象,因此Doctrine将$entBRef视为必须持久化的新实体。

我们的解决方案是通过我不太喜欢的同一存储库类来获取所有内容,因为我们正在查询不属于该存储库的事物,因此必须使用同一EM进行持久化/刷新。

如果您有其他解决方案,那就太好了。

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