Variable permanently available in controller Cakephp3

0

I want to have a variable that is filled with a query that is available for all views of my controller in cake php

$number = $this ->Mesas->find('all')
            ->where(['user_id =' => $this->Auth->user('id')])
            ->count(); 

    $this->set('number');
    $this->set('_serialize', ['number']);

I want to have available the variable $ number for all my views (currently I have copied that code in all my actions) but I just started with Cakephp and I want to learn how to handle it correctly.

    
asked by J.leo 01.11.2017 в 01:04
source

1 answer

0

In the AppController, create the function, I would change the name of the variable to something less frequent and better defined as numberMesas, but I'll give you the example as you have it.

public function beforeRender(Event $event)
{
    $number = TableRegistry::get('Mesas');

    $number = $this ->Mesas->find('all')
        ->where(['user_id =' => $this->Auth->user('id')])
        ->count(); 

$this->set(['number' => $number]);
$this->set('_serialize', ['number']);
}
    
answered by 13.12.2017 в 02:10