I am new to cakephp. I am trying to put a query in after getting the result part in model and use it in call back. But getting errors when I try to get the debug of $newArray from model.. In my controller I have this
function index($var = null){
if (empty($this->data)) {
}
else {
$results = array();
$getRecords = $this->Model->find('all');
$results = $this->Model->afterFind($getRecords);
debug($this->newArray);
}
}
In my Model I have this
class Model extends AppModel {
function afterFind($getRecords){
$newArray = array();
$query_string = $getRecords['Record']['column1']" ;
$results = $this->Model->query($query_string);
foreach($results as $result){
//do something and add to $newArray
}
return $newArray;
}
}
Callbacks are automatically called by Cake, you do not call them by hand (that’s the point, otherwise they’d be normal methods). The flow is:
Model::findModel::beforeFindcallback, if presentModel::afterFindcallback if present, passing in an array of found dataModel::afterFindThere are examples for
afterFindin the manual. You should not trigger further queries fromafterFind, as this may go into an infinite loop, with each query triggering moreafterFindcallbacks. I also wouldn’t suggest to alter your results too drastically in anafterFind, you should only do light massaging of the results where necessary. I can’t tell from your post what it is you’re trying to achieve, so I can’t give any concrete hints, but you can probably do it withoutafterFindby formulating a better query in the controller.