开发者

CakePHP, Containable, Group By

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
                   开发者_如何学C                 [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'));
        }
    }


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.


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:

  • http://debuggable.com/posts/how-to-group-by-in-cakephps-new-release-part-2:4850d2c9-edbc-4f0e-9e0e-07d64834cda3
  • http://nuts-and-bolts-of-cakephp.com/2008/06/03/new-way-to-group-by-in-cakephp
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜