I want to implement shared "add" actions in the AppController. For this, I need to access the appropriate model of the derivated controller.
How do I do this?
I want to implement shared "add" actions in the AppController. For this, I need to access the appropriate model of the derivated controller.
How do I do this?
The primary model class of a controller is stored in $this->modelClass
, so you could do something like this:
class AppController extends Controller {
function _add($data) {
$this->{$this->modelClass}->save($data);
}
}
class PostController extends AppController {
function someFunction() {
$this->_add($data); // saves to Post model
}
}