CodeIgniter 4 无法识别 UserModel

CodeIgniter 4 will not recognize UserModel

我在 Windows 10 上使用 CodeIgniter 4,但遇到 CI 无法识别我的模型的问题。

我在提交表单时收到以下错误消息:

Error
Class 'CodeIgniter\Models\UserModel' not found
APPPATH\Controllers\User.php at line 42

CI 将此行(在 User.php 中)显示为问题的来源:

$model = new UserModel();

这是我的用户控制器文件 (app/Controllers/User.php):

<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\Models\UserModel;

class User extends Controller {
    public function index() {
      return redirect()->to('/user/signin');
    }

    public function signin() {
      $data = [];
      helper(['form']);

      echo view('templates/dashkit/head');
      echo view('templates/dashkit/signin', $data);
      echo view('templates/dashkit/foot');

      return;
    }

    public function register() {
      $data = [];
      helper(['form']);

      if($this->request->getMethod() == 'post') {
        $rules = [
          'company' => 'required|min_length[8]|is_unique[users.user_company]',
          'email' => 'required|min_length[11]|max_length[255]|valid_email|is_unique[users.user_email]',
          'firstname' => 'required|min_length[2]|max_length[50]',
          'lastname' => 'required|min_length[2]|max_length[50]',
          'password' => 'min_length[8]|max_length[255]',
          'password_confirm' => 'matches[password]',
        ];

        if(!$this->validate($rules)) {
          $data['validation'] = $this->validator;
        } else {
          // If the information passes validation, add the user to the database
          $model = new UserModel();

          $newData = [
            'company'   => $this->request->getVar('company'),
            'email'     => $this->request->getVar('email'),
            'firstname' => $this->request->getVar('firstname'),
            'lastname'  => $this->request->getVar('lastname'),
            'password'  => $this->request->getVar('password')
          ];
          $model->save($newData);
          $session = session();
          $session->setFlashdata('success', 'The form was successfully submitted.');
          return redirect()->to('/dashboard');
        }
      }

      echo view('templates/dashkit/head');
      echo view('templates/dashkit/register', $data);
      echo view('templates/dashkit/foot');

      return;
    }
}

这是我的 UserModel 文件 (app/Models/UserModel.php):

<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model {
  protected $allowedFields = ['user_company', 'user_created_at', 'user_email', 'user_firstname', 'user_lastname', 'user_password'];
  protected $beforeInsert = ['beforeInsert'];
  protected $beforeUpdate = ['beforeUpdate'];
  protected $createdField  = 'created_at';
  protected $table = 'users';

  protected function beforeInsert(array $data) {
    if(isset($data['data']['password'])) {
      $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
    }

    return $data;
  }

  protected function beforeUpdate(array $data) {


    return $data;
  }
}

首先,您需要遵循 documents 中的命名空间规则:

namespace App\Controllers;   

use App\Models\NewsModel;    // App instead of CodeIgniter
use CodeIgniter\Controller;

其次,您随后收到的错误(如您的评论中所述):

CodeIgniter\Database\Exceptions\DataException "There is no data to insert."

是因为$model->save($newData);没有设置主键造成的

Saving Data:

save(): This is a wrapper around the insert() and update() methods that handle inserting or updating the record automatically, based on whether it finds an array key matching the $primaryKey value