J.B.J.
J.B.J.

Reputation: 430

CakePHP 3.x - User auth-component contains table?

I am using CakePHP's Auth-component for user-login data and want to associate the users_table with a user_details table. The association works, and if I manually get a user out it works fine, but is it possible to make the auth-component load in the associated table when logging the user in? so far I have tried this with no luck:

$this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'contain' => ['user_details']
            ]
        ],
        'loginAction' => [
            'controller' => 'users',
            'action' => 'login'
        ]
    ]);

Note the "contain" part - that is where I try to load associated table but with no luck?

Thanks.

Upvotes: 1

Views: 675

Answers (1)

arilia
arilia

Reputation: 9398

contain has been deprecated in favor of 'finder'

so define a finder in your User Table

public function findDetails($query, array $options)
{
    return $query
        ->contain(['UserDetails']);
}

and in the AppController

$this->loadComponent('Auth', [
    'authorize' => ['Controller'],
    'authenticate' => [
        'Form' => [
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ],
            'finder' => ['details']
        ]
    ],
    'loginAction' => [
        'controller' => 'users',
        'action' => 'login'
    ]
]);

https://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query

Upvotes: 2

Related Questions