Kohana“有很多通过”关系
我想知道编辑表单的“有很多通过”关系的最佳方法是什么。 假设我有一群可以属于多个类别的用户。
该表单将有一些像这样的复选框:
<input type="checkbox" name="category_ids" value="1" />
<input type="checkbox" name="category_ids" value="2" />
然后在我的控制器中我可以做类似的事情:
// dump all relations
DB::delete('users_categories')->where('user_id','=',$user->id)->execute();
// add new relations
foreach (explode(',', $_POST['category_ids']) as $category)
$user->add('category', ORM::factory('category', $category))
但这对我来说看起来太复杂了(也因为我有多个“有很多通过”关系)。有没有更简单/更好的方法来使用 kohana orm 来完成此任务? :)
I was wondering what the best method is to edit a 'has many through' relation with a form.
Let's say I have a bunch of users that can belong to multiple categories.
The form would have some checkboxes like this:
<input type="checkbox" name="category_ids" value="1" />
<input type="checkbox" name="category_ids" value="2" />
Then in my controller I could do something like:
// dump all relations
DB::delete('users_categories')->where('user_id','=',$user->id)->execute();
// add new relations
foreach (explode(',', $_POST['category_ids']) as $category)
$user->add('category', ORM::factory('category', $category))
But this looks too complicated to me (also because I have more than one 'has many through' relations). Is there an easier / better way to accomplish this using kohana orm? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我就是这么做的
thats how i do it
要查找添加的内容(反转参数以查找删除的内容),请考虑使用 array_diff()。
有了这个,你应该能够编写比纯 orm 更有效的代码。
To find what was added (reverse the args to find what was removed) consider using array_diff().
With this you should be able to code something more efficient than pure orm.