原则 2:通过值对象重新附加实体

发布于 2024-12-24 17:19:38 字数 862 浏览 0 评论 0原文

我正在使用 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 实例替换为当前的?

显然,我可以手动创建另一个带有手动合并的 CountryAddress

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-12-31 17:19:38

据我所知,目前还没有记录的方法可以做到这一点。

As far as I know, there is currently no documented way to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文