0

I have two separate CakePHP 3.1 apps that each use the same database, but display the information in very different ways. To reduce code duplication, I'm extracting most of the models into a plugin. The controllers differ wildly from each other, so I am not including any of them in the plugin at the moment. What is the proper way to specify that the primary model for a controller resides in a plugin?

In previous versions of CakePHP, the answer would have been to use the $uses property of the controller, but as far as I can tell this is no longer an option. As best I can tell, the only way to make the plugin model accessible from all actions is to do a loadModel in the controller's initialize method. Is that the optimal way to handle this situation?

paran
  • 69
  • 8
  • 1
    Looking at the source of the Controller class, it would seem that setting `public $modelClass = 'Plugin.Model';` may work. `__construct` determines what it *thinks* would be the model, and then calls `modelAwareTrait::_setModelClass` with that, but that function doesn't actually set it if there's something there already. I haven't tested this, though. – Greg Schmidt Apr 18 '18 at 01:42
  • @GregSchmidt Just tested this out and it works. If you make this into an answer, I'll accept it. – paran Apr 18 '18 at 12:15

1 Answers1

1

Looking at the source of the Controller class, it would seem that setting public $modelClass = 'Plugin.Model'; may work. __construct determines what it thinks would be the model, and then calls modelAwareTrait::_setModelClass with that, but that function doesn't actually set it if there's something there already.

Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35