Tuesday, July 8, 2014

Re: Issue with COUNT() as query field

Always use virtual fields in 2.x when using custom SQL
http://book.cakephp.org/2.0/en/models/virtual-fields.html

Then it will all work out of the box.

mark


Am Dienstag, 8. Juli 2014 12:43:53 UTC+2 schrieb Sam Clauw:
What I'm trying to do is making a self join to get a menu tree structure out of the database. It almost works with this code:

$this->set('cmsPages', $this->CmsPage->find('all', array(
    'fields' => array(
        'CmsPage.name',
        'CmsPage.lft',
        'CmsPage.rgt',
        '(COUNT(CmsPage.name) - 1) as depth'
    ),
    'joins' => array(
        array('table' => 'cms_pages',
            'alias' => 'Parent',
            'type' => 'LEFT',
            'conditions' => array(
                'CmsPage.lft BETWEEN Parent.lft AND Parent.rgt',
                'CmsPage.deleted' => null
            )
        )
    ),
    'group' => array(
        'CmsPage.plugin',
        'CmsPage.controller',
        'CmsPage.action'
    ),
    'order' => array(
        'CmsPage.lft ASC'
    )
)));[/php]

The only thing that doesn't fit my needs is how the array is build when using "count()" as a field:

array(
(int) 0 => array(
'CmsPage' => array(
'name' => 'Add item',
'lft' => '1',
'rgt' => '22',
'plugin' => 'coaster_cms',
'controller' => 'cms_pages',
'action' => 'index'
),
(int) 0 => array(
'depth' => '2',
)
),
...
)

That should be the output that I realy need:

array(
(int) 0 => array(
'CmsPage' => array(
'name' => 'Add item',
'lft' => '1',
'rgt' => '22',
'plugin' => 'coaster_cms',
'controller' => 'cms_pages',
'action' => 'index'
'depth' => '2'
)
)
)
Can anyone help me with this small issue? ;)

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: