tags:

views:

438

answers:

1

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?

+3  A: 

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
    }
}
deceze
Again, very helpful and to the point. Thank you!
blinry