CodeIgniter:数据映射器 ORM delete() 一个关系;

发布于 2024-11-17 02:39:24 字数 700 浏览 4 评论 0原文

我有一个名为 userscountriescountries_users 的表。

文档指出,要删除您执行的简单关系:

// Get user foo
$u = new User();
$u->where('username', 'foo')->get();

// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

// Delete relation between user foo and country Australia
$u->delete($c);

这将从中删除相应的行countries_users 表。

我的问题是,如果我没有相关的 Country() 对象要构造怎么办?

如果国家和用户是一对多的关系,那么当然知道用户名属性就足以解除他与国家的关联。

所有删除函数似乎都需要至少两个对象...使用 DataMapper ORM 函数完成此类关系删除的最佳方法是什么?

I have a tables called users, countries, and countries_users.

The documentation states that to delete a simple relationship you perform:

// Get user foo
$u = new User();
$u->where('username', 'foo')->get();

// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

// Delete relation between user foo and country Australia
$u->delete($c);

This would remove the corresponding row from the countries_users table.

My question is, what if I have no relevant Country() object to construct?

If countries and users are a one-to-many relationship, then certainly knowing the username attribute is enough to disassociate him with a country.

All the delete functions seem to require at least two objects... What is the best way to accomplish the deletion of this type of relation using the DataMapper ORM functions?

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

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

发布评论

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

评论(2

_失温 2024-11-24 02:39:24

“所有删除功能似乎都需要至少两个对象”

不完全正确,可以在单个对象上执行 delete() 而无需显式删除对象的关系,它是自动处理的。

来自用户指南

注意:当您删除一个对象时,它与其他对象的所有关系也将被删除。免费房屋清洁! :)

此外,您可以使用 users 表中的一列作为国家/地区 ID,而不是使用单独的 countries_users 表来建立关系,假设它是一个(国家/地区)对多(用户)关系。

我的问题是,如果我没有要构造的相关 Country() 对象怎么办?

那么你就不用担心任何事情了。如果没有要删除的关系,尝试删除它们不会造成任何损害。

有关系需要删除!我想将 user_id 传递到我的控制器中,并取消他与 states_users 表中的国家/地区的关联。要使用记录的函数完成此操作,我还必须传递country_id...IMO 与此操作无关。

除非您特别想要删除特定关系,否则您不必查找国家/地区 ID。就您而言,您正在处理一种用户只能拥有一个国家/地区的关系,因此您无需指定要删除哪个相关国家/地区。我想到了以下两个选项:

指定一个新国家/地区(删除前一个国家/地区)

$c = new Country();
// Get all countries named "Wonderland"
// Usually we'll use an id instead, there could theoretically be more than one
$c->where('name', 'Wonderland')->get(); 
$user->save($c);

只需删除所有相关国家/地区(当然只有一个)

$c = new Country();
// Get all countries
$c->get();
$user->delete($c); // You may need $c->all here

如果我们正在处理多对多关系,您当然会拥有知道要删除哪些,但由于只有一个 - 将它们全部删除就足够了。

"All the delete functions seem to require at least two objects"

Not totally true, a delete() can be preformed on a single object without the need to explicitly delete the object's relationships, it is handled automatically.

From the user guide:

Note: When you delete an object, all its relations to other objects will also be deleted. Free house cleaning! :)

In addition, you may use a column in the users table for the country id instead of a separate countries_users table for relationships, assuming it is a one(country)-to-many(users) relationship.

My question is, what if I have no relevant Country() object to construct?

Then you don't have to worry about anything. If there are no relationships to delete, attempting to delete them will not cause any harm.

There is a relationship to delete! I want to pass the user_id into my Controller and disassociate him with a country in the countries_users table. To accomplish this using the documented functions, I would have to also pass in the country_id... Which IMO is irrelevant for this operation.

You don't have to look up the country id unless you specifically want to delete a particular relationship. In your case, you're working with a relationship where a user can only have one country, so you don't need to specify which related country to delete. Here are two options off the top of my head:

Assigning a new country (removes the previous one)

$c = new Country();
// Get all countries named "Wonderland"
// Usually we'll use an id instead, there could theoretically be more than one
$c->where('name', 'Wonderland')->get(); 
$user->save($c);

Just delete all related countries (there is only one of course)

$c = new Country();
// Get all countries
$c->get();
$user->delete($c); // You may need $c->all here

If we were working with a many to many relationship, you would of course have to know which ones to delete, but since there is only one - deleting them all is sufficient.

一人独醉 2024-11-24 02:39:24

不管你相信与否,我无法使用韦斯利提供的代码删除这种关系。

然而,这似乎有效:

$u = new User();
$u->where('id', $id)->include_related('country', 'id', TRUE, TRUE)->get();

$c = new Country();
$c->where('id', $u->country->id)->get();
$c->delete($u);

Believe it or not, I was unable to remove the relationship using the code Wesley has provided.

However, this seemed to work:

$u = new User();
$u->where('id', $id)->include_related('country', 'id', TRUE, TRUE)->get();

$c = new Country();
$c->where('id', $u->country->id)->get();
$c->delete($u);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文