CakePHP与外部数据库的CakeDC用户插件:在布尔

时间:2017-10-27 20:29:20

标签: php cakephp plugins cakephp-3.x cakedc

我的应用程序是在CakePHP 3.x中开发的,有2个数据库。

我使用CakedDC Users插件,它可以与默认数据库中的所有模型一起使用。这是我在AppController中的内容:

public function initialize()
{
    parent::initialize();

    $this->loadComponent('Flash');
    $this->loadComponent('RequestHandler', [
        'viewClassMap' => [
            'docx' => 'Word',
        ],
    ]);
    $this->loadComponent('Paginator');
    $this->loadComponent('CakeDC/Users.UsersAuth');
}

public function beforeFilter(Event $event)
{
$userId = $this->Auth->user('id');
    EventManager::instance()->on(new RequestMetadata($this->request, $userId));
    $isLogged = $this->Auth->user();
    $this->set(compact('isLogged'));

    if ($clientId = $this->request->query('client_id')) {
        $client = $this->loadModel('Clients')->find()
            ->where(['id' => $clientId])
            ->select(['id', 'slug', 'last_name', 'first_name'])
            ->first();

        $this->set(compact('client'));
    }
}

public function beforeRender(Event $event)
{
    if (!array_key_exists('_serialize', $this->viewVars) &&
        in_array($this->response->type(), ['application/json', 'application/xml'])
    ) {
        $this->set('_serialize', true);
    }
}

在第二个数据库的控制器中,我有以下功能:

public function initialize()
{

}

public function index()
{
    $connection = ConnectionManager::get('db3'); // 'db2' where my second database is configured 
    $machines = $connection->execute('SELECT * FROM MACHINE');

    $this->set(compact('machines'));
    $this->set('_serialize', ['machines']);
}

验证后,所有模型都正常工作,但在此外部视图中除外。我一直在机器index.ctp中得到这个错误:

以下是我得到的错误:

  

应用\控制器\ AppController-> beforeFilter    CORE \ src \ Event \ EventManager.php,第422行       ⟩蛋糕\ Event \ EventManager-> _callListener       CORE \ src \ Event \ EventManager.php,第391行       ⟩蛋糕\事件\ EventManager->派遣       CORE \ src \ Event \ EventDispatcherTrait.php,第78行       ⟩Cake \ Controller \ Controller-> dispatchEvent       CORE \ src \ Controller \ Controller.php,第497行       ⟩Cake \ Controller \ Controller-> startupProcess       CORE \ src \ Http \ ActionDispatcher.php,第116行       ⟩蛋糕\ Http \ ActionDispatcher-> _invoke       CORE \ src \ Http \ ActionDispatcher.php,第95行       ⟩蛋糕\ Http \ ActionDispatcher->派遣       CORE \ src \ Routing \ Dispatcher.php,第60行       ⟩Cake \ Routing \ Dispatcher-> dispatch       ROOT \ webroot \ index.php,第36行

     

错误:ROOT \ src \ Controller \ AppController.php,第65行   (!)Xdebug:用户在第33行的C:\ wamp64 \ www \ inventory \ src \ Template \ Error \ error500.ctp中触发   调用堆栈

     

时间记忆功能位置

     

1 0.0861 5439240 Cake \ Error \ BaseErrorHandler-> wrapAndHandleException()... \ BaseErrorHandler.php:0   2 0.0888 5445696 Cake \ Error \ BaseErrorHandler-> handleException()... \ BaseErrorHandler.php:164   3 0.0888 5445696 Cake \ Error \ ErrorHandler-> _displayException()... \ BaseErrorHandler.php:180   4 0.0919 5737712 Cake \ Error \ ExceptionRenderer-> render()... \ ErrorHandler.php:144   5 0.0932 5878816 Cake \ Error \ ExceptionRenderer-> _outputMessage()... \ ExceptionRenderer.php:194   6 0.1235 7711592 Cake \ Error \ ExceptionRenderer-> _outputMessage()... \ ExceptionRenderer.php:328   7 0.1235 7711592 Cake \ Controller \ Controller-> render()... \ ExceptionRenderer.php:319   8 0.1255 7791592 Cake \ View \ View-> render()... \ Controller.php:617   9 0.1260 7792544 Cake \ View \ View-> _render()... \ View.php:597   10 0.1261 7793376 Cake \ View \ View-> _evaluate()... \ View.php:973   11 0.1263 7822312 include(' C:\ wamp64 \ www \ inventory \ src \ Template \ Error \ error500.ctp')... \ View.php:1014   12 0.1275 7840560 xdebug_print_function_stack()... \ error500.ctp:33

我该怎么办?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的第二个控制器具有空initialize功能。因此,Auth组件未加载(由AppController::initialize完成,但从未调用过),因此当您尝试在$this->Auth中使用beforeFilter时},它不存在。你的第二个initialize即使在那里也有充分的理由吗?消除它,或者在其中调用parent::initialize(),无论哪种方式都可以确保加载所需的组件。