在 CakePHP 中使用 saveAll() 保存多个 hasMany 数据,而无需在视图中写入 id
我终于使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您有允许编辑自己的事件的用户。如果是这种情况,最简单的方法是添加验证规则来验证是否允许用户编辑提交的计划。
在您的操作中,在调用 save() 之前,将当前用户 ID 注入每个记录中。即:
这可能不完全有效,但应该可以让你接近。在您的 Schedule 模型中,添加验证规则:
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 may not work exactly, but should get you close. In your Schedule model, add a validation rule: