CodeIgniter Forums
Cant Load Model In Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Cant Load Model In Controller (/thread-80272.html)

Pages: 1 2


Cant Load Model In Controller - Nomie7 - 10-11-2021

Hi,
I'm unable to load Model in the Controller I tried to follow the tutorials but don't know where I messed up or what am I doing wrong.

RecordModel.php
PHP Code:
<?php

namespace App\Models;
use 
CodeIgniter\Model;

class 
RecordModel extends Model
{
    //protected $db;
    protected $table      'records';
    protected $primaryKey 'id';

    protected $useAutoIncrement true;

    protected $returnType    'array';
    protected $useSoftDeletes false;

    protected $allowedFields = ['name''email'];

    protected $useTimestamps true;
    protected $createdField  'created_at';

    protected $validationRules    = [];
    protected $validationMessages = [];
    protected $skipValidation    false;


And this is the controller


PHP Code:
<?php

namespace App\Controllers;

use 
App\Models\RecordModel;

class 
Home extends BaseController
{
    public function index()
    {
        // return view('welcome_message');

        $records = new RecordModel();
        $data $records->findAll();
        print_r($data);
    }


this is the error i get

Code:
Error
Class 'App\Models\RecordModel' not found
APPPATH/Controllers/Home.php at line 13



RE: Cant Load Model In Controller - paulbalandan - 10-11-2021

Where did you save your RecordModel?


RE: Cant Load Model In Controller - InsiteFX - 10-12-2021

Did you save with character case as the class name? They have to match character case.


RE: Cant Load Model In Controller - Nomie7 - 10-12-2021

(10-11-2021, 07:33 PM)paulbalandan Wrote: Where did you save your RecordModel?

app/models/RecordsModel.php
InsiteFX Wrote:Did you save with character case as the class name? They have to match character case.
yes you can see above in my code


RE: Cant Load Model In Controller - includebeer - 10-12-2021

File permission?


RE: Cant Load Model In Controller - ikesela - 10-12-2021

RecordsModel.php : rename to RecordModel.php to match class name and namespace


RE: Cant Load Model In Controller - Kenneth Kipchumba - 10-12-2021

(10-12-2021, 04:52 AM)Nomie7 Wrote:
(10-11-2021, 07:33 PM)paulbalandan Wrote: Where did you save your RecordModel?

app/models/RecordsModel.php


In your controller, you are using RecordModel but your model file name is app/models/RecordsModel.php. You can choose to rename your file to RecordModel.php or use RecordsModel.php in your controller.



RE: Cant Load Model In Controller - Nomie7 - 10-13-2021

Ok we got that solved by changing the name. I'm new so please help me understand this error now.
Here is code
PHP Code:
        $record = new RecordsModel();
        $data $record->findAll();
        print_r($data); 

Here is the error

Code:
Error
Call to a member function getResult() on bool

Im trying to follow the docs but dont how am i keep getting it wrong


RE: Cant Load Model In Controller - includebeer - 10-13-2021

(10-13-2021, 05:11 AM)Nomie7 Wrote: Ok we got that solved by changing the name. I'm new so please help me understand this error now.
Here is code
PHP Code:
        $record = new RecordsModel();
        $data $record->findAll();
        print_r($data); 

Here is the error

Code:
Error
Call to a member function getResult() on bool

Im trying to follow the docs but dont how am i keep getting it wrong

Something must be wrong in your model (table name, column name, …) or in your database credentials in the configuration file because findAll() should work. Double check everything.


RE: Cant Load Model In Controller - Nomie7 - 10-13-2021

check everything nothing is wrong checked, database name, table name, connection details and everything else. still the same. even make a new table new model file.