If knowledge is power, then sharing knowledge is ultimate powertrip
Latest Updates
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 *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top