如何使学说不在数据库中保留重复的对象?

发布于 2024-12-25 13:11:57 字数 589 浏览 1 评论 0原文

我有两种不同类型的对象:行程位置

行程有一个起点目的地,它们都是Location对象。

位置不会指向骑行

这意味着我在教义上具有多对一的单向关系。

如何使原则确保我的数据库中没有任何重复的 Location 对象?

例如: 如果我创建从明尼苏达州明尼阿波利斯到明尼苏达州曼凯托的 Ride 并保留它们,我现在已在数据库中存储了一个 Ride 和两个 Location 对象。

现在这些都已保留,我将创建另一个从明尼苏达州曼凯托到明尼苏达州新乌尔姆的行程并保留它们。

Doctrine 复制了明尼苏达州曼凯托的位置

学说是否具有此功能,或者我有责任检查我是否正在创建重复的对象?

I have two different kinds of objects: Ride and Location.

A Ride has an origin and a destination which are Location objects.

Location does not point back to the Ride.

This means I have a many-to-one uni-directional relationship in doctrine.

How can I make doctrine ensure that I don't have any duplicate Location objects in my database?

For Example:
If I create a Ride from Minneapolis, MN to Mankato, MN and persist them I now have stored one Ride and two Location objects in my database.

Now that those are persisted, I go to create another Ride from Mankato, MN to New Ulm, MN and persist them.

Doctrine has duplicated the Location Mankato, MN.

Does doctrine have this functionality, or is it my responsibility to check if I am creating duplicate objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜灵血窟げ 2025-01-01 13:11:57

您能否对位置名称(可能是州名称+城市名称)施加唯一约束?

您有责任查看该位置是否已存在。如果是这样,获取它并将其提供给您的 Ride 对象。

class Ride {
   private $from;
   private $to;

   public function setFrom(Location $from) {$this->from = $from;}

   public function setTo(Location $to) {$this->to = $to;}
 }

Can you put a unique constraint on Location Name (Maybe state name + city name)?

It's your responsibility to see if the location already exists. And if so, fetch it and supply it to your Ride object.

class Ride {
   private $from;
   private $to;

   public function setFrom(Location $from) {$this->from = $from;}

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