0
votes

My app is developed in CakePHP 3.x, and has 2 databases.

I use CakedDC Users plugin, and it works fine with all models from the default database. Here is what I have in 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);
    }
}

In the controller from the second database, I have these functions:

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']);
}

After authentication, all models are working fine, except in this external view. I keep getting this error in machines index.ctp:

Here is the errors I get:

App\Controller\AppController->beforeFilter CORE\src\Event\EventManager.php, line 422 ⟩ Cake\Event\EventManager->_callListener CORE\src\Event\EventManager.php, line 391 ⟩ Cake\Event\EventManager->dispatch CORE\src\Event\EventDispatcherTrait.php, line 78 ⟩ Cake\Controller\Controller->dispatchEvent CORE\src\Controller\Controller.php, line 497 ⟩ Cake\Controller\Controller->startupProcess CORE\src\Http\ActionDispatcher.php, line 116 ⟩ Cake\Http\ActionDispatcher->_invoke CORE\src\Http\ActionDispatcher.php, line 95 ⟩ Cake\Http\ActionDispatcher->dispatch CORE\src\Routing\Dispatcher.php, line 60 ⟩ Cake\Routing\Dispatcher->dispatch ROOT\webroot\index.php, line 36

Error in: ROOT\src\Controller\AppController.php, line 65 ( ! ) Xdebug: user triggered in C:\wamp64\www\inventory\src\Template\Error\error500.ctp on line 33 Call Stack

Time Memory Function Location

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

What should I do exactly ?

Thanks in advance.

1
You should tell us exactly where the error happens, preferably including a stack trace.Greg Schmidt
thanks Greg. I just added in my questionmbenjemaa
So, the error is at line 65 of your AppController. Which line is that?Greg Schmidt
the line 65 contains: $userId = $this->Auth->user('id');mbenjemaa
What I understand is that the problem from using 2 databases: index.ctp is related to a table in the second databse, and the $userId is from a table in a the first database.mbenjemaa

1 Answers

0
votes

Your second controller has an empty initialize function. As a result, the Auth component is not loaded (that's done by AppController::initialize, but it's never called), so when you try to use $this->Auth in beforeFilter, it doesn't exist. Is there a good reason why your second initialize is even there? Eliminate it, or else call parent::initialize() in it, either way would ensure that your required components are loaded.