Reputation: 35
I am using CodeIgniter 4 on Windows 10 and I'm having an issue where CI is not recognizing my model.
I am receiving the following error upon form submission:
Error
Class 'CodeIgniter\Models\UserModel' not found
APPPATH\Controllers\User.php at line 42
CI is displaying this line (in User.php) as the source of the issue:
$model = new UserModel();
Here is my User controller file (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;
}
}
And here is my UserModel file (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;
}
}
Upvotes: 1
Views: 2821
Reputation: 7997
First you need to follow the namespace rules as in the documents:
namespace App\Controllers;
use App\Models\NewsModel; // App instead of CodeIgniter
use CodeIgniter\Controller;
Second, the error you receive consequently (as mentioned in your comment):
CodeIgniter\Database\Exceptions\DataException "There is no data to insert."
is caused because there is no primary key set for $model->save($newData);
see 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
Upvotes: 2