CakePHP 具有多个外键的有序行为
据我所知,CakePHP 没有内置的机制来处理记录的重新排序。因此,我使用 有序行为据我所知,这是 CakePHP 中重新排序的事实上的行为。到目前为止,当我将其添加到各种模型时,它运行良好,但是,我遇到了一种情况,我不确定如何处理。
我的模型层次结构如下。
部分>标题>类别> Item
但是,Items 可以直接附加到部分:
Section > Item
Item 模型的表同时定义了 category_id
和 section_id
,尽管任何给定记录实际上只使用一个。
当您设置模型的 $actsAs
时,有序行为会设置两个参数。这是我的标题模型的一个:
var $actsAs = array('Ordered' => array('field' => 'order','foreign_key' => 'section_id'));
如何为具有两个外键 section_id
和 category_id
的 Item 模型定义 $actsAs
成员确保正确维护顺序/顺序?
CakePHP does not have a built in mechanism for handling reordering of records, as far as I know. So, I'm using the Ordered Behavior that, as near as I can tell, is the defacto behavior for reordering in CakePHP. So far it's working well as I add it to various models, however, I've got a situation I'm not sure how to deal with.
My model hierarchy is as follows.
Section > Heading > Category > Item
However, Items can be attached directly to sections:
Section > Item
The Item model's table has both category_id
and section_id
defined, though only one is actually used for any given record.
The ordered behavior has two parameters set when you set the model's $actsAs
. Here's the one for my Heading model:
var $actsAs = array('Ordered' => array('field' => 'order','foreign_key' => 'section_id'));
How should I define the $actsAs
member for the Item model where it has two foreign keys section_id
and category_id
to ensure that the ordering/sequence is properly maintained?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有对此进行广泛的测试,但在我的第一次尝试中,这似乎是一种解决方法,可以让我动态管理排序。在我的
ItemsController
内部,我有以下操作:我的模型没有通过
$actsAsforeign_key
)代码> 成员。相反,在进行任何订单操作之前,我会在运行时使用适当的foreign_key
确定父项类型并附加行为。I haven't tested this extensively, but with my first try this appears to be a workaround that will let me dynamically manage the ordering. Inside of my
ItemsController
I've got the following actions:My model doesn't set the ordered behavior (and thus the
foreign_key
) via the$actsAs
member. Instead, prior to any order manipulation, I determine the parent item type and attach the behavior, with appropriateforeign_key
at run time.