codeigniter 4 中的错误您必须将数据库 table 设置为用于您的查询

error in codeigniter 4 You must set the database table to be used with your query

我正在使用 codeigniter 4。我正在尝试通过在模型中创建方法从数据库中获取数据,但出现错误

You must set the database table to be used with your query

即使我在查询生成器中提到了 table 名称。我不知道为什么会这样?以下是我的代码

 public function Login($values)
    {
        $db = \Config\Database::connect();
  
        $result= $db->table('tbl_adminuser')
                     ->where(['username',$values['username']])
                     ->where(['password',$values['password']])
                     ->get()
                     ->getResult();
        print_r($result);
    }

为用户创建模型

<?php namespace Myth\Auth\Models;

use CodeIgniter\Model;
use Myth\Auth\Authorization\GroupModel;
use Myth\Auth\Entities\User;

class UserModel extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';

    protected $returnType = User::class;
    protected $useSoftDeletes = false;

    protected $allowedFields = [
        'email', 'username', 'password_hash', 'reset_hash', 'reset_at', 'reset_expires', 'activate_hash',
        'status', 'status_message', 'active', 'force_pass_reset', 'permissions',
        'first_name',
        'last_name',
        'image',
        'address',
        'phone',
        'email',
        'gender',
        'country',
        'city',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $useTimestamps = false;

    protected $validationRules = [
        'email' => 'if_exist|required|valid_email|is_unique[users.email,id,{id}]',
        'phone' => 'if_exist|required|is_unique[users.phone,id,{id}]',
        'username' => 'if_exist|required|alpha_numeric_punct|min_length[3]|max_length[30]|is_unique[users.username,id,{id}]',
        'password_hash' => 'if_exist|required',
    ];
    protected $validationMessages = [];
    protected $skipValidation = false;

    protected $afterInsert = ['addToGroup'];

    /**
     * The id of a group to assign.
     * Set internally by withGroup.
     *
     * @var int|null
     */
    protected $assignGroup;

    /**
     * Logs a password reset attempt for posterity sake.
     *
     * @param string $email
     * @param string|null $token
     * @param string|null $ipAddress
     * @param string|null $userAgent
     */
    public function logResetAttempt(string $email, string $token = null, string $ipAddress = null, string $userAgent = null)
    {
        $this->db->table('auth_reset_attempts')->insert([
            'email' => $email,
            'ip_address' => $ipAddress,
            'user_agent' => $userAgent,
            'token' => $token,
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

    /**
     * Logs an activation attempt for posterity sake.
     *
     * @param string|null $token
     * @param string|null $ipAddress
     * @param string|null $userAgent
     */
    public function logActivationAttempt(string $token = null, string $ipAddress = null, string $userAgent = null)
    {
        $this->db->table('auth_activation_attempts')->insert([
            'ip_address' => $ipAddress,
            'user_agent' => $userAgent,
            'token' => $token,
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

    /**
     * Sets the group to assign any users created.
     *
     * @param string $groupName
     *
     * @return $this
     */
    public function withGroup(string $groupName)
    {
        $group = $this->db->table('auth_groups')->where('name', $groupName)->get()->getFirstRow();

        $this->assignGroup = $group->id;

        return $this;
    }

    /**
     * Clears the group to assign to newly created users.
     *
     * @return $this
     */
    public function clearGroup()
    {
        $this->assignGroup = null;

        return $this;
    }

    /**
     * If a default role is assigned in Config\Auth, will
     * add this user to that group. Will do nothing
     * if the group cannot be found.
     *
     * @param mixed $data
     *
     * @return mixed
     */
    protected function addToGroup($data)
    {
        if (is_numeric($this->assignGroup)) {
            $groupModel = model(GroupModel::class);
            $groupModel->addUserToGroup($data['id'], $this->assignGroup);
        }

        return $data;
    }


}

太用模型了

$model = 新用户模型();

$model->asObject()->where('active', '1')->findAll();

从数据库获取数据有两种方式。

  1. 使用模型文件
  2. 在控制器文件中加载数据库

检查以下两种类型以从数据库中获取数据。

1.使用模型文件

首先,创建一个用户模型并提及数据库的名称table。

模型文件示例如下:

<?php 
namespace App\Models;
     
use CodeIgniter\Model;
    
class UserMasterModel extends Model
{
    protected $table   = 'user_master';
}  

然后创建一个控制器文件并使用 use App\Models\UserMasterModel; 加载模型。 您应该遵循以下控制器文件示例,其中提到加载模型并使用模型文件获取数据。

<?php 
namespace App\Controllers;
use CodeIgniter\Controller;

use App\Models\UserMasterModel;

public function __construct()
{
    $this->db = \Config\Database::connect();
}

public function Login($values){
    $UserMasterModel = new UserMasterModel();
    $result = $UserMasterModel->where('username',$values['username'])
                                ->where('password',$values['password'])
                                ->findAll();
    print_r($result);
}

2。在控制器文件中加载数据库

使用 \Config\Database::connect() 时,控制器文件如下所示:

<?php 
namespace App\Controllers;
use CodeIgniter\Controller;

public function __construct()
{
    $this->db = \Config\Database::connect();
    $this->user_master = $this->db->table('user_master');
}

public function Login($values){
    $this->user_master->select('*');
    $this->user_master->where('username',$values['username']);
    $this->user_master->where('password',$values['password']);
    $result = $this->user_master->get()->getResult();

    print_r($result);
}