Reputation: 212
I have the following structure:
Category hasMany CategoryDynamic
When I use the Containable behavior, I want to get the CategoryDynamic
data sorted by the CategoryDynamic.language
field. For example, instead of the default structure:
array(
'Category' => array(
// ...
),
'CategoryDynamic' => array(
'0' => array(
// ...
),
'1' => array(
// ...
),
),
);
I want to get them sorted by the CategoryDynamic.language
field:
array(
'Category' => array(
// ...
),
'CategoryDynamic' => array(
'eng' => array(
// ...
),
'fra' => array(
// ...
),
),
);
Can CakePHP do this by default, or do I need to modify the results in the afterFind
function?
Upvotes: 0
Views: 72
Reputation: 610
Cakephp performs such solution if you use contain.
It can sort data from 'hasMany' relationship but doesn't return exact the way you have written.
CategroyModel:
class CategroyModel extends Model {
public $actsAs = array('Containable');
public $hasMany = array('CategoryDynamic');
public function getData(){
$conditions['contain'] = array(
'CategoryDynamic' => array(
'order' => 'CategoryDynamic.language ASC'
)
);
return $this->find('first', $conditions);
}
}
CategoryDynamicModel:
class CategoryDynamicModel extends Model {
public $actsAs = array('Containable');
public $belongsTo = array('Categroy');
}
Then when you call
$this->Categroy->getData();
in a controller, it will give sorted CategoryDynamic:
array(
'Category' => array(
// ...
),
'CategoryDynamic' => array(
0 => array(
language => 'eng'
// ...
),
1 => array(
language => 'fra'
// ...
),
...
),
);
I can't run and check the exact code I posted above since I don't have the project or schema, but I have done some similar thing in my project and it has worked.
Upvotes: 1