If knowledge is power, then sharing knowledge is ultimate powertrip
You are here: Home >> Frameworks >> Cakephp >> Self join in CakePHP

Self join in CakePHP

I tried to explain how to do a self join in CakePHP

I use this in my Gallery Model:
var $hasMany = array(
‘Galleries’ => array(
‘className’ => ‘Gallery’,
‘order’ => ‘Galleries.ord ASC’,
‘foreignKey’ => ‘parent_id’));
var $belongsTo = array(
‘Galleries’ => array(
‘className’ => ‘Gallery’,
‘order’ => ‘Galleries.ord ASC’,
‘foreignKey’ => ‘parent_id’));
First, note the ‘Galleries’ (you change that association name for any
other, just dont use the model name). Second, you might want to know
more about $this->Model->findAllThreaded() . It returns a *VERY*
useful array , with [‘child’] and everything. I *heart* CakePHP 🙂

class Category extends AppModel
{
var $name = ‘Category’;
var $belongsTo = array(‘Categories’ => array(‘className’ => ‘Category’,’order’ =>’Category.id DESC’ ,’foreignKey’ => ‘parent_catg’));

}

About Jawed Shamshedi

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top