I have the following model:
App::uses('AppModel', 'Model');
class Sync extends AppModel {
public $belongsTo = array(
'Unit' => array(
'className' => 'Sync',
'foreignKey' => 'parent_id'
)
);
public $hasMany = array(
'Consultant' => array(
'className' => 'Sync',
'foreignKey' => 'parent_id'
)
);
}
If I do a $this->Sync->find('all') I get back an array almost the way I want it:
Array
(
[0] => Array
(
[Sync] => Array
(
[id] => 22222
[parent_id] => 22222
[statistic_date] => 2012-11-14 00:00:00
)
[Unit] => Array
(
[id] => 22222
[parent_id] => 22222
[statistic_date] => 2012-11-14 00:00:00
)
[Consultant] => Array
(
[0] => Array
(
[id] => 11111
[parent_id] => 22222
[statistic_date] => 2011-12-15 00:00:00
)
[1] => Array
(
[id] => 33333
[parent_id] => 22222
[statistic_date] => 2011-12-14 00:00:00
)
)
)
)
The problem is I want to do a condition on the consultants. Like this:
$this->Sync->Consultant->find('all', array(
'conditions' => array(
'Consultant.statistic_date BETWEEN ? AND ?' => 'array(
'2011-12-01 00:00:00', '2011-12-31 00:00:00'
)'
)
))
However it does not give me a desired array back:
Array
(
[0] => Array
(
[Unit] => Array
(
[id] => 22222
[parent_id] => 22222
[statistic_date] => 2012-11-14 00:00:00
)
[Consultant] => Array
(
[id] => 11111
[parent_id] => 22222
[statistic_date] => 2011-11-15 00:00:00
)
)
[1] => Array
(
[Unit] => Array
(
[id] => 22222
[parent_id] => 22222
[statistic_date] => 2012-12-14 00:00:00
)
[Consultant] => Array
(
[id] => 33333
[parent_id] => 22222
[statistic_date] => 2011-12-14 00:00:00
)
)
)
I want it on this form:
Array
(
[0] => Array
(
[Unit] => Array
(
[id] => 22222
[parent_id] => 22222
[statistic_date] => 2012-11-14 00:00:00
)
[Consultant] => Array
(
[0] => Array
(
[id] => 11111
[parent_id] => 22222
[statistic_date] => 2011-12-15 00:00:00
)
[1] => Array
(
[id] => 33333
[parent_id] => 22222
[statistic_date] => 2011-12-14 00:00:00
)
)
)
)
How do you achieve that? Possible solutions I can think of is by using contain or the Hash:: class, but do not know how.
Any help in the matter will be greatly appreciated.
Ok, I solved the issue. This is what I did. I bind the model on the fly:
This gives me: