How to transfer only the first level of the hierarchy of objects using Doctrine?

I am transferring DBAL ZF3 applications to Doctrine and want to go step by step. I am currently using a hierarchy of objects Mapper. Each object in the hierarchy FooEntityhas a meaning FooMapper. Saving nested objects is performed by nested Mappers. Each Mapperspreserves its essence with Zend\Db\Sql\Insertor, Zend\Db\Sql\Updateand calls the correct one Mapperfor sub-arguments, such as BarMapperfor BarEntity.

Now, before I start with convenient Doctrine functions, such as cascade={"persist"}, I want to save the hierarchy Mapperand just save the top level of the nested object with persist(...)and flush().

But when I try to do it

public function save(AbstractDataObject $dataObject)
{
    $newLogicalConnection = $this->logicalConnectionMapper->save($dataObject->getLogicalConnection());
    $newUser = $this->userMapper->save($dataObject->getUser());

    $dataObject->setLogicalConnection($this->entityManager->find(LogicalConnection::class, $newLogicalConnection->getId()));
    $dataObject->setUser($this->entityManager->find(User::class, $newUser->getId()));

    $this->entityManager->persist($dataObject);
    $this->entityManager->flush();

    return $dataObject;
}

I get an error

A new entity was found through the relationship 'MyNamespace\DataObject\AbstractEndpoint#externalServer' that was not configured to cascade persist operations for entity: MyNamespace\DataObject\ExternalServer@000000006098ccff0000000068c23676. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist  this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'MyNamespace\DataObject\ExternalServer#__toString()' to get a clue.

, Doctrine, , -, . ? cascade , Doctrine .

Doctrine , ? ?

+4
1

, ( ) AbstractEndpoint->externalServer, cascade={"persist"}

, ExternalServer AbstractEndpoint->externalServer, cascade={"persist"}

, Doctrine , . - , .

, :

  • $this->entityManager->persist($externalServer); ExternalServer
  • AbstractEndpoint->externalServer cascade={"persist"}. , , , , .

:

? , Doctrine .

- Doctrine ExternalServer . . , Doctrine -

+1

Source: https://habr.com/ru/post/1686408/


All Articles