I’m new to cakephp
I’m having a very basic question.
I want to pass a parameter that is the result of a query model inside the controller to use it in view
For example in CODE IGNITER:
Controller:
$data= array(
'title'=>'Main',
'today'=>date('d/m/Y'),
'user_id'=>$this->AdminModel->getUserId()->result(),
);
$this->load->view('admin', $data);
Inside my controller I want to use the result of $ user_Id to make a condition
I did something like
$user_id = $this->Users->find()->where([‘Users.name’ => ‘Francisco’]);
$this->set(‘users’, $users);
but I can not use the result inside my controller
How can I use $user_id[‘Users’][‘id’]); OR $user_id->name in controller?
dakota
September 18, 2017, 6:04pm
2
You almost have it
$user = $this->Users->find()->where([‘Users.name’ => ‘Francisco’])->first();
$this->set(‘users’, $users);
Then you can access $user->name
in the view
~]
Nice,
but how can I use result $user->name in Controller?
Actually, I need use the result for load may templates according to the query result for exeample:
I do not know if it’s the best way but I thought of something like
In my table Users
id, name, template
In my controller:
$user_template = $this->Users->find()->where([‘Users.id ’ => $session->read(‘Users.id ’);]);
$this->viewBuilder()->setLayout($user->user_template);
OR
$this->viewBuilder()->setLayout($user_template);
I don’t know…
dakota
September 18, 2017, 6:19pm
4
The same
$user = $this->Users->find()->where([‘Users.id’ => $this->Auth->user('id)])->first();
$this->viewBuilder()->setLayout($user->user_template);
I would recommend that you work through the tutorials https://book.cakephp.org/3.0/en/tutorials-and-examples.html