原则 2:通过值对象重新附加实体
我正在使用 Doctrine 2,并且有一个 Address
值对象:
class Address
{
/** @var string */
protected $street;
/** @var string */
protected $city;
/** @var Application\Domain\Model\Country */
protected $country;
}
我需要将此 Address
存储在 PHP 会话中(序列化),并稍后检索它。然后,当我检索此值对象时,我希望将 Country
对象合并到当前的实体管理器,以便该国家/地区与当前工作单元同步。
是否可以将此值对象“合并”到当前实体管理器,就像我使用 cascade="merge"
对常规实体所做的那样,以获取 Country
实例替换为当前的?
显然,我可以手动创建另一个带有手动合并的 Country
的 Address
:
$address = $_SESSION['address'];
$country = $em->merge($address->getCountry());
$address = new Address($address->getStreet(), $address->getCity(), $country);
但我想知道 Doctrine 中是否缺少一个允许我直接合并 VO 的功能反而?
I'm working with Doctrine 2, and have an Address
value object:
class Address
{
/** @var string */
protected $street;
/** @var string */
protected $city;
/** @var Application\Domain\Model\Country */
protected $country;
}
I need to store this Address
in a PHP session (serialized), and retrieve it later on. When I then retrieve this value object, I want to have the Country
object merged to the current Entity Manager, so that this Country is in sync with the current unit of work.
Is it possible to "merge" this value object to the current Entity Manager, as I would do on a regular entity with cascade="merge"
, to get the Country
instance replaced with the current one?
I can obviously manually create another Address
with a manually merged Country
:
$address = $_SESSION['address'];
$country = $em->merge($address->getCountry());
$address = new Address($address->getStreet(), $address->getCity(), $country);
But I'm wondering if I'm missing a feature in Doctrine that would allow me to directly merge the VO instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,目前还没有记录的方法可以做到这一点。
As far as I know, there is currently no documented way to do that.