blinry
blinry

Reputation: 4965

CakePHP: AppController to access model of derivated Controller

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?

Upvotes: 9

Views: 2299

Answers (1)

deceze
deceze

Reputation: 522024

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
    }
}

Upvotes: 22

Related Questions