CakePHP ->; HABTM->控制器视图选择多个值并以正确的顺序保存到数据库中
我使用 CakePHP 2.0,有两个模型:user
和 course
。 它们与HABTM相连,因此是多对多的关系。
我可以创建、阅读、更新和删除课程,这一切都没有问题。 但现在,用户可以参加一些课程。
在用户的添加视图中,我可以使用这些代码行选择许多课程,因为它创建了一个多选字段。
$this->set('courses', $this->User->Course->find('list'));
echo $this->Form->input('Course');
所以我可以选择多个值,但问题是顺序!我的课程需要一个订单,应用程序的用户应该能够管理该订单(课程的开始/结束日期不相关)。
示例:我有 id = 10 的用户。 应该可以不选择任何课程,或者以正确的顺序选择一门或多门课程。 我有大约。 200 个 ID 为 1-200 的课程 所以应该是可能的:
* UserId 10 -> no course
* UserId 10 -> CourseId 23
* UserId 10 -> CourseId 23, CourseId 11, CourseId 45, CourseId 10, CourseId 199 (the order is important)
我如何在我的添加/编辑视图和我的控制器中执行此操作来处理这个问题?
此致。
I use CakePHP 2.0 and I have two models: user
and course
.
They are connected with HABTM, so it is a many-to-many relationship.
I can create, read, update and delete courses, that's all okay.
But now, the user can take part in some courses.
In the add view of the user, I can select many courses with these lines of code, because it creates a multiple select field.
$this->set('courses', $this->User->Course->find('list'));
echo $this->Form->input('Course');
So I can select multiple values, but the problem is the order! I need a order in my courses, which the user of the application should be able to manage (the start/end date of the course is not relevant).
Example: I have the user with the id = 10.
It should be possible to select no course or 1 or many with a correct order.
I have approx. 200 courses with the IDs 1-200
So it should be possible:
* UserId 10 -> no course
* UserId 10 -> CourseId 23
* UserId 10 -> CourseId 23, CourseId 11, CourseId 45, CourseId 10, CourseId 199 (the order is important)
How can I do this in my add / edit view and in my controller to handle this?
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在课程表中,您必须添加另一列,例如相关课程。
您应使用此列作为约束。您可以以 JSON 格式或序列化等形式存储数据。您可以使用列中的数据在查找查询中对课程进行排序。
然后,在视图中,必须使用 javascript 来限制选择错误的课程并通知用户有关订单的信息。
这只是一个想法,但我希望它有所帮助!
In your courses table, you'll have to add another column, let's say related courses.
You shall use this column as a constraint. You can store data either in JSON format or serialized, etc. You can use the data in the column to sort your courses in the find query.
Then, in the view, you must use javascript to restrict the selection of the wrong course and notify the user about the order.
It's just an idea, but I hope it helps!