开发者

cakephp how to use call back method

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 AppM开发者_C百科odel {
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:

  • Controller calls Model::find
  • Cake triggers Model::beforeFind callback, if present
  • Cake finds results
  • Cake triggers Model::afterFind callback if present, passing in an array of found data
    • the callback is supposed to return data
  • Controller receives the data returned by Model::afterFind

There are examples for afterFind in the manual. You should not trigger further queries from afterFind, as this may go into an infinite loop, with each query triggering more afterFind callbacks. I also wouldn't suggest to alter your results too drastically in an afterFind, 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 without afterFind by formulating a better query in the controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜