Cakephp - Overriding model method from controller
I need to override & add methods in a model invoked by a controller. I don't want to write anything inside Model class file because it's used by other controll开发者_开发百科ers too. Methods are like pagination()
and find()
, can I do it from inside a controller?
CakePHP behaviors are mixins. They add methods to a model, which is what you are looking for.
It sounds like dynamically attaching a behavior to the model would get you the outcome you need.
Looking at Model::__construct()
, I can see that it calls $this->Behaviors->init($this->alias, $this->actsAs);
.
You may be able to call it again after the model has been instantiated to attach different behaviors (ie. $this->MyModel->Behaviors->init('MyModel', array('MyBehavior'));
).
In fact, a closer look reveals that $this->MyModel->Behaviors
is an instance of BehaviorCollection
. As such, you can use the attach() method.
精彩评论