0
votes

I have model and entity in my CakePHP(latest) app that I want to extend in this way. I want to tell the cake to use ExtraStuff model class instead of app Stuff

App\Model\Table\Stuff

MyPlugin\Model\Table\ExtraStuff

I'm trying to redefine model in the main controller in beforeRender method with methods from Cake\Datasource\ModelAwareTrait

/**
 * Override a existing callable to generate repositories of a given type.
 *
 * @param string $type The name of the repository type the factory function is for.
 * @param callable $factory The factory function used to create instances.
 * @return void
 */
 modelFactory($type, callable $factory)

Or how I can do it? I have a lot of $this->loadModel('Stuff') calls inside the project and I need to tell the app use ExtraStuff class when I need. Thanks.

1

1 Answers

0
votes

Look this https://book.cakephp.org/3.0/en/controllers.html#loading-additional-models

If you are using a table provider other than the built-in ORM you can link that table system into CakePHP’s controllers by connecting its factory method:

// In a controller method.
$this->modelFactory(
    'ElasticIndex',
    ['ElasticIndexes', 'factory']
);

After registering a table factory, you can use loadModel to load instances:

// In a controller method.
$this->loadModel('Locations', 'ElasticIndex');