CakePHP:在两个表上登录

x3naxklr  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(146)

我有一个CakePHP网站,有一个新的数据库结构,我想重新配置登录。首先,这是我的错误Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.email' in 'where clause'
我有2个表,它们通过UUID字段连接。
| 使用者|用户数据|
| - -|- -|
| 识别码|识别码|
| 通用唯一标识符|通用唯一标识符|
| 口令|电子邮件|
在旧网站中,电子邮件和密码在同一个表中,但现在不在了。我用bake插件创建了Table / Entitys。最后我在UsersTable.php中设置了$this->hasOne('Userdata');,在UserdataTable.php中设置了$this->hasOne('Users');
UserdataTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class UserdataTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('userdata');
        $this->setDisplayField('ID');
        $this->setPrimaryKey('ID');
        $this->hasOne('Users');
    }
}

UserTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('ID');
        $this->setPrimaryKey('ID');
        $this->hasOne('Userdata');
    }
}

这是在我的AppController.php中

$this->loadComponent('Auth', [
            'authorize'=> 'Controller',
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
             //use isAuthorized in Controllers
            'authorize' => ['Controller'],
             // If unauthorized, return them to page they were just on
            'unauthorizedRedirect' => $this->referer()
        ]);

然后在我的UsersController.php中

public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('Your username or password is incorrect.');
        }
    }

我使用的是CakePHP版本3.6.15

oknwwptz

oknwwptz1#

这里您需要的是一个自定义查找器,以便您可以修改用于查找用户记录的查询。
最简单、最向前兼容的方法可能是在查询中简单地包含Userdata关联,并配置表单验证器的username选项以包括别名,例如Userdata.email,而不仅仅是email
在您的UsersTable类中:

public function findWithUserdata(\Cake\ORM\Query $query, array $options): \Cake\ORM\Query
{
    return $query->contain('Userdata');
}

在您的Auth组件配置中:

'authenticate' => [
    'Form' => [
        'fields' => [
            'username' => 'Userdata.email', // << this one here
            'password' => 'password'
        ],
        'finder' => 'withUserData', // << and that one
    ]
],

然后,这将为您提供一个沿着所示的查找查询:

SELECT
    ...
FROM
    users Users
LEFT JOIN
    userdata Userdata ON Userdata.UUID = Users.UUID
WHERE
    Userdata.email = 'some@email.address'

另请参阅

*操作手册〉控制器〉组件〉AuthComponent〉配置身份验证处理程序
*Cookbook〉控制器〉组件〉验证组件〉自定义查找查询
*Cookbook〉数据库访问和ORM〉检索数据和结果集〉自定义查找器方法

相关问题