CakePHP Auth Retrieves Additional Data

I'm just learning CakePHP so excuse me

I am using CakePHP 1.2.5 Auth component with UserController. The user model contains two tables:

class User extends AppModel { var $name = 'User'; var $belongsTo = 'Company'; } 

When login () is called, I see that the data is retrieved in the SQL log (LEFT JOIN is executed), so the model seems correct, but Auth only saves the data from the users table and drops everything else. How can I get company data later without making an extra request?

+4
source share
3 answers

Or, if you don't like modifying a base library like me, you can also do ...

in action UsersController.login

 function login() { if ($this->Auth->user()) { $Session->write('Company', $this->User->Company->findById($this->Auth->user('id'))); $this->redirect($this->Auth->redirect()); } } 

You can get company information using $this->Session->read('Company.name') on your controller and $session->read('Company.name') in your views. Remember to add the Session component and helper.

+4
source

in cake / libs / controller / components / auth.php on line 819, it should be

 $data = $model->find(array_merge($find, $conditions), null, null, 0); 

try changing it to

 $data = $model->find(array_merge($find, $conditions), null, null, 1); 

basically set recursive to 1; you may need to do this in some other places.

+2
source

Better than changing the kernel, try restraining behavior: http://book.cakephp.org/view/474/Containable . At the bottom of the page there is an example user / profile /.

+1
source

Source: https://habr.com/ru/post/1299211/


All Articles