0
votes

I have the following Doctrine entities where EntityA is the owning side:

<?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;
    // ....
}

I do also have a RepositoryB class:

<?php 

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

Then in my class I am trying to persist an EntityA object:

<?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();
    }
}

But I've got the following error:

A new entity was found through the relationship 'EntityA#propertyB' that was not configured to cascade persist operations for entity: EntityB. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade

Here is how my doctrine.yaml file looks like:

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

Last but not least I do have the following setup at 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'

What I have done so far?

  1. Read a lot of post here on SO leading to the same solution add cascade={"persist"} to the owning side which I did and I must say did not work.
  2. Read Doctrine docs looking for mistakes on my entities definition but so far everything looks fine to me.

I did found this which is helpful but I keep questioning myself why would Doctrine tries to insert and existent entity just because of the reference? Is there any way to get this working?

I do not want the EntityB persisted nor updated, it already exists. I do want the new EntityA have a FK to an existent record in EntityB.

1
Is there any other code involved? Anything special in your setters?Jeroen
@Jeroen no, what you see in the OP is exactly what I have in my code (ofc no with those names the ones here are just examples)ReynierPM
whats in " setEntityB()"?Jeroen
@Jeroen just a setter. I have added it to the OP.ReynierPM
happy to take a look if you set up a demo repository.Jeroen

1 Answers

0
votes

During the weekend a work colleague did work very hard to find out what was causing the issue and after a lot of hours debugging how Doctrine and the UoW works he found where the problem was: how Doctrine and the Repository pattern works. Let me explain a little bit.

If you check this closer and go deep inside Doctrine you will notice how the following code leads to two different entities manager:

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

So:

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

Is being fetched through another EM and since I am using $this->entityManager to persist/flush the recently created entityA object then Doctrine sees $entBRef as a new entity that has to be persisted.

Our solution was to fetch everything through the same repository class which I do not like so much because we are querying things that does not belongs there and we had to ever persist/flush using the same EM.

If you have any other solution it is more than welcome.