简体   繁体   English

SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'Users.email'

[英]SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.email' in 'where clause'

i'm working in cakephp 4. i have set the model ('Representatives') for login authentication but it is using another model with name users for authentication.我在 cakephp 4 工作。我已经设置了 model('代表')进行登录身份验证,但它使用另一个 model 和名称用户进行身份验证。 i have uploaded the AppController.php file as well as EmployeesController.php file where I defined the method(action).我已经上传了 AppController.php 文件以及EmployeesController.php 文件,我在其中定义了方法(操作)。

in Controller/AppController.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"
        ]
    ]);
}

in EmployeesController:在员工控制器中:

 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");
  }

in model/entity/Representative.php在模型/实体/代表.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);
    }
}
}

in model/table/RepresentativesTable.php在模型/表格/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);
    }
  }
 }

By default, cakephp uses the user model for authentication默认情况下,cakephp 使用用户 model 进行认证

You can find an example of authentication using another route here in this video您可以在此视频中找到使用其他路由的身份验证示例

In the example, it is defined as follows:在示例中,它定义如下:

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;
    }    

Although the video translations are terrible.虽然视频翻译很糟糕。 I was able to follow the example in the video, the important part was adding the 'resolver' to allow cakephp to find the right table.我能够按照视频中的示例进行操作,重要的部分是添加“解析器”以允许 cakephp 找到正确的表。

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

This did create another problem with $this->Identiy->get failing, I expect will do add a resolver for this to work as expected also.这确实造成了 $this->Identiy->get 失败的另一个问题,我希望会添加一个解析器以使其也能按预期工作。

Thanks !!谢谢 !!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:SQLSTATE[42S22]:未找到列:1054 &#39;where 子句&#39;中的未知列 &#39;0&#39; - ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'名称' - SQLSTATE[42S22]: Column not found: 1054 Unknown column ' name' in 'where clause' SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'fighter.id_fighter' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fighter.id_fighter' in 'where clause' 如何获取SQLSTATE [42S22]:找不到列:1054在我的情况下,&#39;where子句&#39;中的未知列&#39;$ email&#39;已解决 - how to get SQLSTATE[42S22]: Column not found: 1054 Unknown column '$email' in 'where clause' solved in my case 错误:SQLSTATE [42S22]:找不到列:1054 where子句中的未知列 - Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column in where clause Magento:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ billing_name” - Magento: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'billing_name' in 'where clause' CakePHP:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ Post” - CakePHP: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post' in 'where clause' SQLSTATE [42S22]:找不到列:laravel 中“where 子句”中的 1054 未知列“0” - SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' in laravel SQLSTATE [42S22]:找不到列:1054 Yii 1.1中“ where子句”中的未知列“ registration” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'registration' in 'where clause' in Yii 1.1 SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ answers.question_id” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'answers.question_id' in 'where clause'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM