插件模型中的CakePHP 3 defaultConnectionName不起作用 [英] CakePHP 3 defaultConnectionName in plugin model doesn't work
问题描述
我基于cakephp 3框架和作为插件的管理面板创建一个门户,以将文件与其余文件分开.这些表将仅覆盖我放置在单独创建的数据库"admin"中的管理面板.
I create a portal based on the framework cakephp 3 and admin panel made as a plugin to separate the files from the rest. The tables, which will cover only the admin panel I put in separately created database 'admin'.
我已经在config/app.php中正确配置了数据库连接.
I have correctly configured database connections in config/app.php.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'portal',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
],
'admin' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'database' => 'admin_portal',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
],
],
所以我有插件Admin.
So I have plugin Admin.
我在插件2控制器内部创建了
I created inside plugin 2 controllers:
plugins/Admin/src/Controller/AdminController.php
plugins/Admin/src/Controller/AdminController.php
<?php
namespace Admin\Controller;
use Cake\Controller\Controller;
use Cake\ORM\TableRegistry;
class AdminController extends Controller {
public function initialize() {
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
}
}
plugins/Admin/src/Controller/MainController.php
plugins/Admin/src/Controller/MainController.php
<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class MainController extends AdminController {
public function dashboard() {
$this->loadModel('Messages');
$results = $this->Messages->find('all');
pr($results);
}
}
然后我为表Messages创建了模型
And I created model for table Messages
<?php
namespace Admin\Model\Table;
use Admin\Model\Entity\Message;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class MessagesTable extends Table {
public function initialize(array $config)
{
parent::initialize($config);
$this->table('messages');
$this->displayField('name');
$this->primaryKey('id');
}
public static function defaultConnectionName()
{
return 'admin';
}
}
当我从MainController触发仪表板操作而不是消息表的结果时,出现错误:
When I fire dashboard action from MainController instead of results from messages table I got an error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal.messages' doesn't exist
因此CakePHP在默认数据库而不是"admin_portal"表中查找表消息.
So the CakePHP is looking for the table messages in the default database instead of in the "admin_portal" table.
我做错了什么?蛋糕在某种程度上不尊重插件模型中的defaultConnectionName()方法. 我尝试在/src(不在插件中)的某些模型中使用此方法-然后它可以正常工作.
What am I doing wrong? Somehow cake doesn't respect defaultConnectionName() method in plugin model. I tried use this method in some model in /src (not in plugin) - then it worked correctly.
推荐答案
您实际上不使用插件模型,您的
You are actually not using your plugin model, your
$this->loadModel('Messages');
调用将(尝试)加载应用程序级别Messages
模型.如果要加载插件模型,请使用插件/点表示法语法,即
call will (try to) load the application level Messages
model. If you want to load plugin models, use the plugin/dot notation syntax, ie
$this->loadModel('Admin.Messages');
另请参见 Cookbook>插件>插件模型
See also Cookbook > Plugins > Plugin Models
这篇关于插件模型中的CakePHP 3 defaultConnectionName不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!