I have to fetch data from posts
table
which has foreign keys
(
category_id
references to categories.id
created_by
references to users.id
,
updated_by
references to users.id
)
I can fetch created_by
username
but not both
$this->Post->Behaviors->load('Containable');
$this->paginate = array(
'conditions' => array('Post.category_id' => $id),
'order' => array('title'),
'contain' => array(
'User'=>array(
'fields'=>array('id','first_name','last_name','username'),
'conditions' => array('User.id = Post.created_by')
),
//posts table has 2 fields(created_by & updated_by) associated with users table
//'User'=>array(
// 'fields'=>array('id','first_name','last_name','username'),
// 'conditions' => array('User.id = Post.updated_by')
// ),
'Category'=>array(
'Type'=>array(
'fields'=>array('id','type_name')
)
),
)
);
//post model
public $belongsTo = array(
'User'=> array(
'className' => 'User',
'foreignKey' => 'created_by',
'foreignKey' => 'updated_by'
),
);
//user model
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => array('created_by','updated_by'),
),
);
how to show both and alias both Users as (created_by
& updated_by
)