Monday, June 16, 2014

Re: How to self join?

Well, it seems there are 2 problems with you suggested query. Here's the SQL error I've got:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CmsPage.null' in 'on clause'
SQL Query:  SELECT `CmsPage`.`id`, `CmsPage`.`parent_id`, `CmsPage`.`name`, `CmsPage`.`lft`, `CmsPage`.`rgt`, `CmsPage`.`plugin`, `CmsPage`.`controller`, `CmsPage`.`action`, `CmsPage`.`show`, `CmsPage`.`sequence`, `CmsPage`.`created`, `CmsPage`.`modified`, `CmsPage`.`deleted`, `Parent`.`id`, `Parent`.`parent_id`, `Parent`.`name`, `Parent`.`lft`, `Parent`.`rgt`, `Parent`.`plugin`, `Parent`.`controller`, `Parent`.`action`, `Parent`.`show`, `Parent`.`sequence`, `Parent`.`created`, `Parent`.`modified`, `Parent`.`deleted` FROM `blwfun`.`cms_pages` AS `CmsPage` LEFT JOIN `blwfun`.`cms_pages` AS `Parent` ON (`CmsPage`.`null` = `Parent`.`id` AND `CmsPage`.`lft` BETWEEN `Parent`.`lft` and `Parent`.`rgt`) WHERE `CmsPage`.`deleted` IS NULL LIMIT 20

So, I changed my model to:

class CmsPage extends CoasterCmsAppModel
{
    public $belongsTo = array(
        'Parent' => array(
            'className' => 'CoasterCms.CmsPage',
            /*'foreignKey' => 'null',*/
            'conditions' => array(
                'CmsPage.lft BETWEEN Parent.lft and Parent.rgt'
            )
        )
    );
    
    public $virtualFields = array(
        'depth' => '(COUNT(Parent.name) - 1)'
    );
}

The only problem is now that I only got one row in return, with a wrong depth value...
Somebody who knows what I'm still doing wrong? Or somebody who know how I can output all SQL statements as a string? The only code I can find is:

$log = $this->Model->getDataSource()->getLog(false, false); debug($log);

But I cannot do anything with that output:

array(  	'log' => array(),  	'count' => (int) 0,  	'time' => null  )

--
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: