PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] CakePHP 3.x - User auth-component contains table?

 January 18, 2022     cakephp, php     No comments   

Issue

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.


Solution

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



Answered By - arilia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing