The question probably sounds quite odd, and indeed it is. Here’s the problem: I have a model, FollowingStationLine, and its corresponding table, following_station_lines. In the model, I am trying to save a record with $this->save(array('field' => 'value));. When I hit the corresponding page, the method seems to be calling in an infinite loop (I’ve put some debug()s before and after the save statement) and it ends only when the memory limit has been reached. I tried even with 2GB of memory, and still the same issue.
This happens also when calling the method from another controller, other than FollowingStationLinesController. All other models/tables work as expected, beside this one. I really can’t imagine what is the problem. Here are the codes for the controller, respectively the model:
Controller:
App::uses('AppController', 'Controller');
class FollowingStationLinesController extends AppController {
public function admin_set() {
$this->FollowingStationLine->set(array(1));
}
}
Model:
App::uses('AppModel', 'Model');
class FollowingStationLine extends AppModel {
public function set($lineIds = array()){
if(!is_array($lineIds)){
return false;
}
$save = array();
$save[] = array('station_id' => 45);
debug($save[0]);
$this->save($save[0]);
}
}
Any help would be highly appreciated!
CakePHP’s AppModel class has a function called ‘set’ that is called by the model’s ‘save’ function. By overrriding ‘set’, you are causing a loop between the 2 methods.
Rename your function to something else and you should be ok.
You can inspect the AppModel source for more info: https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php
UPDATE:
@mark made a very good comment that if your PHP configuration included error reporting mode E_STRICT, you would have received an error for overriding with a mismatched signature.
More info to set your configuration: http://php.net/manual/en/migrating5.errorrep.php