PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] CodeIgniter 4 will not recognize UserModel

 March 09, 2022     codeigniter, codeigniter-4, php     No comments   

Issue

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

Solution

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



Answered By - Vickel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing