CodeIgniter:数据映射器 ORM delete() 一个关系;
我有一个名为 users、countries 和 countries_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不完全正确,可以在单个对象上执行
delete()
而无需显式删除对象的关系,它是自动处理的。来自用户指南:
此外,您可以使用 users 表中的一列作为国家/地区 ID,而不是使用单独的
countries_users
表来建立关系,假设它是一个(国家/地区)对多(用户)关系。那么你就不用担心任何事情了。如果没有要删除的关系,尝试删除它们不会造成任何损害。
除非您特别想要删除特定关系,否则您不必查找国家/地区 ID。就您而言,您正在处理一种用户只能拥有一个国家/地区的关系,因此您无需指定要删除哪个相关国家/地区。我想到了以下两个选项:
指定一个新国家/地区(删除前一个国家/地区)
只需删除所有相关国家/地区(当然只有一个)
如果我们正在处理多对多关系,您当然会拥有知道要删除哪些,但由于只有一个 - 将它们全部删除就足够了。
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:
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.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.
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)
Just delete all related countries (there is only one of course)
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.
不管你相信与否,我无法使用韦斯利提供的代码删除这种关系。
然而,这似乎有效:
Believe it or not, I was unable to remove the relationship using the code Wesley has provided.
However, this seemed to work: