【问题标题】:CakePHP replace app model with model from pluginCakePHP 用插件中的模型替换应用程序模型
【发布时间】:2018-04-16 19:41:17
【问题描述】:

我想以这种方式扩展我的 CakePHP(latest) 应用程序中的模型和实体。我想告诉蛋糕使用 ExtraStuff 模型类而不是应用程序 Stuff

App\Model\Table\Stuff

MyPlugin\Model\Table\ExtraStuff

我正在尝试使用 Cake\Datasource\ModelAwareTrait

中的方法在 beforeRender 方法中重新定义主控制器中的模型
/**
 * 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)

或者我该怎么做?我在项目中有很多 $this->loadModel('Stuff') 调用,我需要告诉应用程序在需要时使用 ExtraStuff 类。 谢谢。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    看这个 https://book.cakephp.org/3.0/en/controllers.html#loading-additional-models

    如果您使用的是内置 ORM 以外的表提供程序,您可以通过连接其工厂方法将该表系统链接到 CakePHP 的控制器:

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

    注册表工厂后,可以使用loadModel加载实例:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多