Kohana“有很多通过”关系

发布于 2024-09-12 18:59:38 字数 618 浏览 8 评论 0原文

我想知道编辑表单的“有很多通过”关系的最佳方法是什么。 假设我有一群可以属于多个类别的用户。

该表单将有一些像这样的复选框:

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

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

发布评论

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

评论(2

故事↓在人 2024-09-19 18:59:38

我就是这么做的

// C
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role)
{
    $action = isset($form['user']['roles'][$role->id]) ? 'add' : 'remove';

    // you dont need this if-statement if you'r using ko2
    if ($action === 'add' && $user->has('roles', $role))
    {
        continue;
    }

    $user->$action('roles', $role);
}

// V
<?
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role):
?>
    <?= form::checkbox('user[roles]['.$role->id.']', $role->id, $user->has('roles', $role)) ?>
    <?= form::label('user_roles_'.$role->id, $role->name) ?>
    <br />
<? endforeach ?>

thats how i do it

// C
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role)
{
    $action = isset($form['user']['roles'][$role->id]) ? 'add' : 'remove';

    // you dont need this if-statement if you'r using ko2
    if ($action === 'add' && $user->has('roles', $role))
    {
        continue;
    }

    $user->$action('roles', $role);
}

// V
<?
$roles = ORM::factory('role')->find_all();
foreach ($roles as $role):
?>
    <?= form::checkbox('user[roles]['.$role->id.']', $role->id, $user->has('roles', $role)) ?>
    <?= form::label('user_roles_'.$role->id, $role->name) ?>
    <br />
<? endforeach ?>
紧拥背影 2024-09-19 18:59:38

要查找添加的内容(反转参数以查找删除的内容),请考虑使用 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.

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