【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.email' in 'where clause'SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'Users.email'
【发布时间】:2021-05-17 09:55:56
【问题描述】:

我在 cakephp 4 工作。我已经为登录身份验证设置了模型(“代表”),但它使用另一个名称为用户的模型进行身份验证。我已经上传了 AppController.php 文件以及EmployeesController.php 文件,我在其中定义了方法(操作)。

在 Controller/AppController.php 中

 class AppController extends Controller
 {
   public function initialize(): void
   {
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');       
    $this->loadComponent('Auth', [
        "authenticate" => [
            "Form" => [
                "fields" => [
                    "username" => "email",
                    "password" => "password"
                ],
                "userModel" => "Representatives"
            ]
        ],
        "loginAction" => [
            "controller" => "Employees",
            "action" => "login"
        ],
        "loginRedirect" => [
            "controller" => "Employees",
            "action" => "home"
        ],
        "logoutRedirect" => [
            "controller" => "Employees",
            "action" => "login"
        ]
    ]);
}

在员工控制器中:

 class EmployeesController extends AppController
  {
     public function initialize(): void
     {
      parent::initialize();
      $this->loadModel("Employees");
      $this->loadModel("Representatives");
      $this->loadComponent('Flash');
    }

public function login()
  {
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        // echo "<pre>"; print_r($user); die; 
        if ($user) {
            $this->Auth->setUser($user);
            echo "<pre>"; print_r($user); exit(); 
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }
    $this->viewBuilder()->setLayout('home');
    $this->set("title", "Login");
  }

在模型/实体/Representative.php中

<?php

namespace App\Model\Entity;

use Authentication\PasswordHasher\DefaultPasswordHasher;
use Cake\ORM\Entity;

class Representative extends Entity
 {
  protected $_accessible = [
    
    'id' => false,
    'name' => true,
    'role' => true,
    'email' => true,
    'password' => true,
    'phone' => true,
    'slug' => false
];

protected function _setPassword(string $password) : ?string
{
    if (strlen($password) > 0) {
        return (new DefaultPasswordHasher())->hash($password);
    }
}
}

在模型/表格/RepresentativesTable.php中

 <?php
 namespace App\Model\Table;

 use Cake\ORM\Table;
 use Cake\Utility\Text;
 use Cake\Event\EventInterface;

 class RepresentativesTable extends Table
 {
  public function initialize(array $config) :void
   {
  

    $this->setTable("newusers");
    


 }


  public function beforeSave(EventInterface $event, $entity, $options)
  {
    if ($entity->isNew()  && !$entity->slug) {
        $sluggedTitle = Text ::slug($entity->name);
        $entity->slug = substr($sluggedTitle, 0, 191);
    }
  }
 }

【问题讨论】:

    标签: php cakephp cakephp-4.x


    【解决方案1】:

    cakephp默认使用用户模型进行认证

    您可以在此video 中找到使用其他路由的身份验证示例

    示例中定义如下:

    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
        {
            $authenticationService = new AuthenticationService([
                'unauthenticatedRedirect' => '/cinema4/usuarios/login',
                'queryParam' => 'redirect',
            ]);
        
            // Load identifiers, ensure we check email and password fields
            $authenticationService->loadIdentifier('Authentication.Password', [
                'fields' => [
                    'username' => 'login',
                    'password' => 'senha',
                ],
                'resolver' => [
                    'className' => 'Authentication.Orm',
                    'userModel' => 'Usuarios'
                ],        
            ]);
        
            // Load the authenticators, you want session first
            $authenticationService->loadAuthenticator('Authentication.Session');
            // Configure form data check to pick email and password
            $authenticationService->loadAuthenticator('Authentication.Form', [
                'fields' => [
                    'username' => 'login',
                    'password' => 'senha',
                ],
                'loginUrl' => '/cinema4/usuarios/login',
            ]);
        
            return $authenticationService;
        }    
    

    【讨论】:

      【解决方案2】:

      虽然视频翻译很糟糕。我能够按照视频中的示例进行操作,重要的部分是添加“解析器”以允许 cakephp 找到正确的表。

      ,
                  'resolver' => [
                      'className' => 'Authentication.Orm',
                      'userModel' => 'YourUserTable'
                  ],  
      

      这确实造成了 $this->Identiy->get 失败的另一个问题,我希望会添加一个解析器以使其也能按预期工作。

      谢谢!!

      【讨论】:

        猜你喜欢
        • 2021-03-29
        • 2023-04-07
        • 2019-08-18
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 2020-03-01
        • 2020-06-08
        • 1970-01-01
        相关资源
        最近更新 更多