【问题标题】:Doctrine - map entity for ORM and ODMDoctrine - ORM 和 ODM 的映射实体
【发布时间】:2018-04-24 23:03:12
【问题描述】:

我正在处理从一个数据库到另一个数据库的同步实体。我为 ORM 和 ODM 映射了实体,例如:

/*
 * @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

效果很好,但我想从 mongo db 的文档管理器中加载实体并将其保存到 ORM:

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

但我对关系有疑问。如何在合并产品时保留相关实体(例如 ProductAction)?

【问题讨论】:

标签: php mongodb doctrine-orm orm


【解决方案1】:

如果我理解正确,您希望在将产品实体合并到 ORM 时将“ProductAction”相关实体合并到 ORM。

您可以在关系上使用 , cascade={"merge"}。例如。

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

Understanding cascade operations

Merging entities

【讨论】:

    猜你喜欢
    • 2011-12-04
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多