Keith Power
Keith Power

Reputation: 14141

cakePHP get users details of logged in user

I am using the cakeManager plugin with cakePHP 3 to manage users. I have two user roles businesses and employees and I have tables for each with additional information.

When a user logs in I am trying to get the additional information with the users details but I cannot seem to get the associated tables with users.

Business Model:

/**
 * Businesses Model
 */
class HotelsTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    $this->table('businesses');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Employees', [
        'foreignKey' => 'hotel_id'
    ]);
}

Extended Users Model from the plugin

namespace App\Model\Table;

use CakeManager\Model\Table\UsersTable as BaseUsersTable;
use Cake\Validation\Validator;

/**
 * Users Model
 */
class UsersTable extends BaseUsersTable
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->hasOne('Employees', [
            'foreignKey' => 'user_id'
        ]);
        $this->hasOne('Businesses', [
            'foreignKey' => 'user_id'
        ]);
    }
}

I have added the Businesses associations directly into plugin also to check is it an issue with the model extension.

Looking at the returned object I see the Roles model associations are also not returned so I suspect it is the plugins find is set to not retrieve assertions.

My problem is I don't know how to override this.

Upvotes: 0

Views: 245

Answers (1)

Bob
Bob

Reputation: 848

As of CakePHP 2.2 (http://bakery.cakephp.org/articles/lorenzo/2012/07/01/cakephp_2_2_and_2_1_4_released), the AuthComponent now accepts the 'contain' key for storing extra information in the session. I have checked with 3.0 and it still supports contain

'Auth' => [ 
   'authorize' => 'Controller', 
   'userModel' => 'CakeManager.Users', 
   'authenticate' => [ 
      'Form' => [ 
         'contain' => [ 'Roles' ],
   ]
]

Look at this example to see how to change the default AuthComponent configurations.

Reference: https://gitter.im/cakemanager/cakephp-cakemanager?at=556389082e564d1f3abefc04

Upvotes: 2

Related Questions