CakePHP,可容纳,分组依据

发布于 2024-11-24 03:57:37 字数 7641 浏览 3 评论 0 原文

我有一个场景,我尝试在与我的主模型关联的模型上使用可包含的分组。

这是我的输出:

    Array
(
    [Subfirm] => Array
        (
            [id] => 1
            [firm_id] => 1
            [name] => Word Spam Management Fund
            [description] => There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            [active] => yes
            [created] => 2011-07-14 18:05:01
            [modified] => 2011-07-14 18:05:01
        )

    [Firm] => Array
        (
            [id] => 1
            [name] => Word Spam
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dictum neque a arcu ultricies semper. Curabitur lacinia, nisi ac cursus mattis, diam ipsum egestas eros, id pharetra dui quam vel sapien. Fusce tempus mauris eu ligula rutrum rutrum. Cras sit amet metus nisl. Nam feugiat malesuada justo, lacinia mollis nulla iaculis sed. Sed dui sapien, aliquam vel vestibulum eget, suscipit ac dui. Donec et neque dapibus lorem euismod convallis. Sed nec mauris vel velit venenatis fermentum et non velit. Pellentesque tincidunt, nisi vitae sollicitudin malesuada, lectus orci egestas neque, vitae placerat magna nisi eu nulla. Donec eleifend laoreet ligula vitae faucibus. Donec eget felis eros. Nullam ligula nisl, porttitor at viverra a, auctor ullamcorper orci. Fusce vestibulum turpis mollis velit tristique luctus.
            [website] => http://www.wordspam.com
            [active] => yes
            [created] => 2011-07-11 11:33:46
            [firmtype_id] => 1
            [aum] => 600.77
            [modified] => 2011-07-10 22:55:16
            [Firmtype] => Array
                (
                    [id] => 1
                    [name] => Hedge Fund
                    [active] => yes
                    [created] => 2011-07-12 12:30:42
                    [modified] => 2011-07-12 12:30:42
                )

        )

    [Subfirmdetail] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 1
                    [value] => 400
                    [date] => 2011-07-14
                    [created] => 2011-07-14 19:09:42
                    [modified] => 2011-07-14 19:09:42
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 1
                            [firmtype_id] => 1
                            [name] => AUM
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 2
                    [value] => 2.34
                    [date] => 2011-07-14
                    [created] => 2011-07-14 19:09:42
                    [modified] => 2011-07-14 19:09:42
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 2
                            [firmtype_id] => 1
                            [name] => Standard Deviation
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

            [2] => Array
                (
                    [id] => 3
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 1
                    [value] => 370
                    [date] => 2011-07-11
                    [created] => 2011-07-11 12:00:00
                    [modified] => 2011-07-11 12:00:00
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 1
                            [firmtype_id] => 1
                            [name] => AUM
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

        )

)

我想按 SubFirmDetail 进行分组,并且仅根据 subFirmDetailType 显示最新的 SubFirmDetail。这是我当前在控制器中使用的代码:

function view($id = null) {
                $this->Subfirm->recursive = -1;
                $data = $this->Subfirm->find('first', array(
                    'contain' => array(
                        'Subfirmdetail' => array(
                            'Subfirmdetailtype' => array(
                                'Datatype',
                            ),
                        ),
                        'Firm' => array(
                            'Firmtype',
                        ),
                    ),
                    'conditions' => array('Subfirm.id' => $id),
                ));
        $this->set('subfirm', $data);
        if (!$id) {
            $this->Session->setFlash(__('Invalid subfirm', true));
            $this->redirect(array('action' => 'index'));
        }
    }

I've got a scenario where I am trying to use group by on a model associated with my main model using containable.

Here is my output:

    Array
(
    [Subfirm] => Array
        (
            [id] => 1
            [firm_id] => 1
            [name] => Word Spam Management Fund
            [description] => There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            [active] => yes
            [created] => 2011-07-14 18:05:01
            [modified] => 2011-07-14 18:05:01
        )

    [Firm] => Array
        (
            [id] => 1
            [name] => Word Spam
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dictum neque a arcu ultricies semper. Curabitur lacinia, nisi ac cursus mattis, diam ipsum egestas eros, id pharetra dui quam vel sapien. Fusce tempus mauris eu ligula rutrum rutrum. Cras sit amet metus nisl. Nam feugiat malesuada justo, lacinia mollis nulla iaculis sed. Sed dui sapien, aliquam vel vestibulum eget, suscipit ac dui. Donec et neque dapibus lorem euismod convallis. Sed nec mauris vel velit venenatis fermentum et non velit. Pellentesque tincidunt, nisi vitae sollicitudin malesuada, lectus orci egestas neque, vitae placerat magna nisi eu nulla. Donec eleifend laoreet ligula vitae faucibus. Donec eget felis eros. Nullam ligula nisl, porttitor at viverra a, auctor ullamcorper orci. Fusce vestibulum turpis mollis velit tristique luctus.
            [website] => http://www.wordspam.com
            [active] => yes
            [created] => 2011-07-11 11:33:46
            [firmtype_id] => 1
            [aum] => 600.77
            [modified] => 2011-07-10 22:55:16
            [Firmtype] => Array
                (
                    [id] => 1
                    [name] => Hedge Fund
                    [active] => yes
                    [created] => 2011-07-12 12:30:42
                    [modified] => 2011-07-12 12:30:42
                )

        )

    [Subfirmdetail] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 1
                    [value] => 400
                    [date] => 2011-07-14
                    [created] => 2011-07-14 19:09:42
                    [modified] => 2011-07-14 19:09:42
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 1
                            [firmtype_id] => 1
                            [name] => AUM
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 2
                    [value] => 2.34
                    [date] => 2011-07-14
                    [created] => 2011-07-14 19:09:42
                    [modified] => 2011-07-14 19:09:42
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 2
                            [firmtype_id] => 1
                            [name] => Standard Deviation
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

            [2] => Array
                (
                    [id] => 3
                    [subfirm_id] => 1
                    [subfirmdetailtype_id] => 1
                    [value] => 370
                    [date] => 2011-07-11
                    [created] => 2011-07-11 12:00:00
                    [modified] => 2011-07-11 12:00:00
                    [active] => yes
                    [Subfirmdetailtype] => Array
                        (
                            [id] => 1
                            [firmtype_id] => 1
                            [name] => AUM
                            [datatype_id] => 1
                            [created] => 2011-07-14 18:09:23
                            [modified] => 2011-07-14 18:09:23
                            [active] => yes
                            [Datatype] => Array
                                (
                                    [id] => 1
                                    [name] => DOUBLE
                                    [created] => 2011-07-14 18:14:58
                                    [modified] => 2011-07-14 18:14:58
                                    [active] => yes
                                )

                        )

                )

        )

)

I'd like to group by SubFirmDetail and only show the newest SubFirmDetail depending on subFirmDetailType. Here is the code I'm currently using in my controller:

function view($id = null) {
                $this->Subfirm->recursive = -1;
                $data = $this->Subfirm->find('first', array(
                    'contain' => array(
                        'Subfirmdetail' => array(
                            'Subfirmdetailtype' => array(
                                'Datatype',
                            ),
                        ),
                        'Firm' => array(
                            'Firmtype',
                        ),
                    ),
                    'conditions' => array('Subfirm.id' => $id),
                ));
        $this->set('subfirm', $data);
        if (!$id) {
            $this->Session->setFlash(__('Invalid subfirm', true));
            $this->redirect(array('action' => 'index'));
        }
    }

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

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

发布评论

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

评论(2

输什么也不输骨气 2024-12-01 03:57:37

我最终不得不在数据库中创建一个视图并输出一个数组,该数组包含在我的条件子句中......对于那些尝试相同的人。

I ended up having to create a view in the database and output an array which I included in my conditions clause... For those attempting the same.

谁的新欢旧爱 2024-12-01 03:57:37

试试这个:

function view($id = null) {
                $this->Subfirm->recursive = -1;
                $data = $this->Subfirm->find('first', array(
                    'contain' => array(
                        'Subfirmdetail' => array(
                            'Subfirmdetailtype' => array(
                                'Datatype',
                            ),
                        ),
                        'Firm' => array(
                            'Firmtype',
                        ),
                    ),
                    'conditions' => array('Subfirm.id' => $id),
                    'group' => 'Subfirmdetail',
                    'order' => 'date',
                ));
        $this->set('subfirm', $data);
        if (!$id) {
            $this->Session->setFlash(__('Invalid subfirm', true));
            $this->redirect(array('action' => 'index'));
        }
    }

或者查看这个链接:

Try this:

function view($id = null) {
                $this->Subfirm->recursive = -1;
                $data = $this->Subfirm->find('first', array(
                    'contain' => array(
                        'Subfirmdetail' => array(
                            'Subfirmdetailtype' => array(
                                'Datatype',
                            ),
                        ),
                        'Firm' => array(
                            'Firmtype',
                        ),
                    ),
                    'conditions' => array('Subfirm.id' => $id),
                    'group' => 'Subfirmdetail',
                    'order' => 'date',
                ));
        $this->set('subfirm', $data);
        if (!$id) {
            $this->Session->setFlash(__('Invalid subfirm', true));
            $this->redirect(array('action' => 'index'));
        }
    }

Or see this links:

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