Try this approach. Override default find('all')
, so that it will accept custom parameter, that will allow reformatting of results. Put this in AppModel
, so it is accessible to all models
Edited base on requirement from comment to remove empty associations and joint data for HABTM associations from reformatted result:
class AppModel extends Model
{
protected function _findAll($state, $query, $results = array())
{
if ($state === 'before') {
return $query;
}
if (isset($query['reformat']) && $query['reformat'] === true) {
foreach ($results as &$_result) {
reset($_result);
$modelName = key($_result);
$modelPart = array_shift($_result);
if (!empty($query['filter']) && is_array($query['filter'])) {
$this->recursive_unset($_result, $query['filter']);
}
$_result = array(
$modelName => array_merge($modelPart, $_result)
);
}
}
return $results;
}
public function getHabtmKeys() {
$habtmKeys = array();
// 1. level inspection
foreach ($this->hasAndBelongsToMany as $name) {
$habtmKeys[] = $name['with'];
}
// 2. level inspection
$allAsssoc = array_merge(
$this->belongsTo,
$this->hasMany,
$this->hasOne
);
foreach ($allAsssoc as $assocName => $assocVal) {
foreach ($this->$assocName->hasAndBelongsToMany as $name) {
$habtmKeys[] = $name['with'];
}
}
return $habtmKeys;
}
private function recursive_unset(&$array, $keys_to_remove) {
foreach ($keys_to_remove as $_key) {
unset($array[$_key]);
}
foreach ($array as $key => &$val) {
if (is_array($val)) {
if (empty($val) || (!isset($val[0]) && empty($val['id']))) {
unset($array[$key]);
} else {
$this->recursive_unset($val, $keys_to_remove);
}
}
}
}
}
To remove empty associations and HABTM joint data, i used recursive unset procedure combined with inspection of associations for relevant model. I was not able to achieve this with configuration of find, contain or any other way.
Next array_shift($_result)
divides $_result
array into two parts - main model on which a find
was run (it is always the first key in result) and the rest (all associations), then it merges these arrays under the key of main model. Of course, this reformats the results only on the first level, but on deeper levels are associations nested by default, so you don't need to care about it.
Now use find('all')
as always, but provide custom reformat
and filter
parameters. If you do not provide them, the result will be fetched in default format.
filter
is array of keys to be removed from result
getHabtmKeys()
dynamically gets array of keys of HABTM associations for model (only 1. and 2. level associations, can be further modified to inspect even deeper).
Usage:
// To nest child associations in parent
$this->Product->find('all', array(
'reformat' => true
));
// To remove also joint data for HABTM and empty associations
$this->Product->find('all', array(
'reformat' => true,
'filter' => $this->Product->getHabtmKeys(),
));
// Keys to remove can be also manually added
$this->Product->find('all', array(
'reformat' => true,
'filter' => array_merge($this->Product->getHabtmKeys(), 'SomeExtraKey'),
));
See also: Retrieving Your Data > Creating custom find types