CakePHP 具有多个外键的有序行为

发布于 2024-12-19 09:46:14 字数 762 浏览 0 评论 0原文

据我所知,CakePHP 没有内置的机制来处理记录的重新排序。因此,我使用 有序行为据我所知,这是 CakePHP 中重新排序的事实上的行为。到目前为止,当我将其添加到各种模型时,它运行良好,但是,我遇到了一种情况,我不确定如何处理。

我的模型层次结构如下。

部分>标题>类别> Item

但是,Items 可以直接附加到部分:

Section > Item

Item 模型的表同时定义了 category_idsection_id,尽管任何给定记录实际上只使用一个。

当您设置模型的 $actsAs 时,有序行为会设置两个参数。这是我的标题模型的一个:

var $actsAs = array('Ordered' => array('field' => 'order','foreign_key' => 'section_id'));

如何为具有两个外键 section_idcategory_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 技术交流群。

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

发布评论

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

评论(1

就此别过 2024-12-26 09:46:14

我还没有对此进行广泛的测试,但在我的第一次尝试中,这似乎是一种解决方法,可以让我动态管理排序。在我的 ItemsController 内部,我有以下操作:

    function moveup($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Item', true));
        } else {
            $item = $this->Item->read(null, $id);

            if ($item['Item']['section_id'] != 0) {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
            } else {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
            }

            if ($this->Item->moveup($id)) {
                $this->Session->setFlash(__('Item moved up', true));
            } else {
                $this->Session->setFlash(__('Item move failed', true));
            }
        }

        $this->redirect($this->referer());
    }

    function movedown($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Item', true));
        } else {
            $item = $this->Item->read(null, $id);

            if ($item['Item']['section_id'] != 0) {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
            } else {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
            }

            if ($this->Item->movedown($id)) {
                $this->Session->setFlash(__('Item moved down', true));
            } else {
                $this->Session->setFlash(__('Item move failed', true));
            }
        }

        $this->redirect($this->referer());
    }

我的模型没有通过 $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:

    function moveup($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Item', true));
        } else {
            $item = $this->Item->read(null, $id);

            if ($item['Item']['section_id'] != 0) {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
            } else {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
            }

            if ($this->Item->moveup($id)) {
                $this->Session->setFlash(__('Item moved up', true));
            } else {
                $this->Session->setFlash(__('Item move failed', true));
            }
        }

        $this->redirect($this->referer());
    }

    function movedown($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Item', true));
        } else {
            $item = $this->Item->read(null, $id);

            if ($item['Item']['section_id'] != 0) {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'section_id'));
            } else {
                $this->Item->Behaviors->attach('Ordered', array('field' => 'order', 'foreign_key'=>'category_id'));
            }

            if ($this->Item->movedown($id)) {
                $this->Session->setFlash(__('Item moved down', true));
            } else {
                $this->Session->setFlash(__('Item move failed', true));
            }
        }

        $this->redirect($this->referer());
    }

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 appropriate foreign_key at run time.

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