0

I'm working on synchronization entities from one DB to another. I have entities mapped for ORM and ODM, like:

/*
 * @ODM\Document(
 *     repositoryClass="App\Lib\Repositories\ProductRepository",
 *     collection="products"
 * )
 * @ODM\InheritanceType("COLLECTION_PER_CLASS")
 *
 * @ORM\Entity(
 *     repositoryClass="App\Lib\Repositories\Legacy\LegacyProductRepository"
 * )
 * @ORM\Table(name="product")
 *
 * @ORM\HasLifecycleCallbacks()
 * @ODM\HasLifecycleCallbacks()
 */
class Product extends Article

It works nice, but I would like to load entity from document manager from mongo db and save it to ORM:

$product = $this->documentManager->find(Product::class, $id);
$this->entityManager->merge($product);
$this->entityManager->flush();

But I have an issue with relations. How do I persist related entity (such as ProductAction) with merging a product?

Petr Jirouš
  • 161
  • 1
  • 15
  • Do you want to merge "ProductAction" also to the ORM when merging a Product? In this case, cascade="merge" might do the trick, https://stackoverflow.com/questions/24612664/understanding-doctrine-cascade-operations or you want to link to the "ProductAction" document in Mongo db? In this case, https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/references.md will do what you want. – Jannes Botis Apr 24 '18 at 07:56

1 Answers1

0

If I understand correctly, you want to merge "ProductAction" related entities to the ORM when a Product entity is merged to it.

You can use , cascade={"merge"} on the relation., eg.

/**
 * @ORM\OneToMany(targetEntity="App\Entity\ProductAction", mappedBy="product", cascade={"persist", "merge"})
 */
private $productActions;

Understanding cascade operations

Merging entities

Jannes Botis
  • 11,154
  • 3
  • 21
  • 39