【发布时间】:2021-04-11 17:16:27
【问题描述】:
我在学说中有两个实体对象:Product、ProductComponent。一个产品可以有多个组件。 首先创建 Product 实体,使用 EntityManager 进行持久化和刷新。 其次,创建产品组件,但不了解真实的产品对象,只知道产品的 id:
<?php
// run kernel..
// Product was persisted before in another class, during same kernel request
$em = ...
$productId = 'some-uuid';
$productReference = $em->getReference(Product::class, $productId);
$productComponent = new ProductComponent();
$productComponent->setProduct($productReference);
$em->persist($productComponent);
$em->flush();
$productRepository = $em->getRepository(Product::class);
$product = $productRepository->find($productId);
$components = $product->getProductComponents();
// $components will be empty ArrayCollection
// but what works:
$componentRepository = $em->getRepository(ProductComponent::class);
$components = $componentRepository->findBy(['product' => $productId]);
// $components will be found with the provided $productId
// end kernel..
当我现在从新内核请求访问$productRepository 时,返回的产品将具有组件关系。
这是什么原因?有什么遗漏吗?
【问题讨论】:
标签: php doctrine-orm doctrine