在 CakePHP 中使用 saveAll() 保存多个 hasMany 数据,而无需在视图中写入 id

发布于 2024-11-05 06:53:45 字数 468 浏览 2 评论 0原文

我终于使用 saveAll() 获得了要保存的 hasMany 数据 - (一个具有许多计划的事件)。

我通过重复 events/admin_edit.ctp 上的 $this->data['Schedule'] 数据并构建与该事件相关的任何/所有时间表的所有字段来使其工作。

这看起来很好(我认为),但我的问题是 - 我必须将 Schedule.id 和 Schedule.event_id 字段添加为隐藏字段,以便它知道要保存哪些数据。这看起来非常不安全/错误...... 1)这是正确的方法吗? 2) 难道有人不能将字段编辑为另一个 ID,然后点击“保存”来更新不同事件的信息吗?

我的下一个假设是,在执行 saveAll() 之前,我必须在控制器中构建某种检查...但是我写得越多,它就会变得越复杂,而且看起来就越不像蛋糕。

非常感谢任何关于如何更好地完成我正在做的事情的想法/建议,或者关于在执行 saveAll() 之前要检查的内容的见解。

I FINALLY got my hasMany data to save using saveAll() - (an Event that hasMany Schedules).

I got it to work by repeating through the $this->data['Schedule'] data on the events/admin_edit.ctp, and building out all the fields for any/all schedules that are related to that event.

This seems fine (I think), but my question/problem is - I had to add the Schedule.id and Schedule.event_id fields as hidden fields so it'd know what data to save. This seems awfully unsecure/wrong... 1) is it the right way to do it? and 2) Couldn't someone just edit the field to another ID, and hit save to update a different event's information?

My next assumption is that I'll have to build in some kind of checks into the controller before doing the saveAll()... but the more I write, the more complicated it's going to get, and the less Cake-like it seems.

Any thoughts/suggestions on how to better do what I'm doing, or insight as to what to check before doing the saveAll() is greatly appreciated.

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

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

发布评论

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

评论(1

她如夕阳 2024-11-12 06:53:45

我假设您有允许编辑自己的事件的用户。如果是这种情况,最简单的方法是添加验证规则来验证是否允许用户编辑提交的计划。

在您的操作中,在调用 save() 之前,将当前用户 ID 注入每个记录中。即:

$this->data['Schedule'][0]['user_id'] = $this->Auth->user('id');

这可能不完全有效,但应该可以让你接近。在您的 Schedule 模型中,添加验证规则:

var $validate = array(
    'user_id' => array(
        'rule' => 'checkAuth'
        'message' => 'Nice try buddy.',
        'on' => 'update'
    )
);

function checkAuth() {
    $authorized = true;
    if(!$this->hasAny(array(
            'Schedule.id'=>$this->data['Schedule']['id'], 
            'Schedule.user_id' => $this->data['Schedule']['user_id']))) {
        $authorized = false;         
    }
    return $authorized;
}

I assume you have users that are allowed to edit their own events. If that's the case, the easiest way is to add a validation rule that verifies that the user is allowed to edit the submitted schedule.

In your action, before the save() is called, inject the current user id into each record. ie:

$this->data['Schedule'][0]['user_id'] = $this->Auth->user('id');

This may not work exactly, but should get you close. In your Schedule model, add a validation rule:

var $validate = array(
    'user_id' => array(
        'rule' => 'checkAuth'
        'message' => 'Nice try buddy.',
        'on' => 'update'
    )
);

function checkAuth() {
    $authorized = true;
    if(!$this->hasAny(array(
            'Schedule.id'=>$this->data['Schedule']['id'], 
            'Schedule.user_id' => $this->data['Schedule']['user_id']))) {
        $authorized = false;         
    }
    return $authorized;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文