Welcome Guest, Not a member yet? Register   Sign In
Creating a new instance of model does seem to work
#1

Hi,
I seem to have an odd problem here. I am not sure why this is happening. I am using version 4.1.8. The following is the problematic code:
Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use APP\Models\MenuModel;
use APP\Models\UsersModel;

class TestSqlite3 extends BaseController
{
    public $usersModel;
    public $menuModel;
    public function __construct()
    {
        // $this->usersModel = model("UsersModel");
        // $this->menuModel = model("MenuModel");
        $this->usersModel  = new UsersModel();
        $this->menuModel = new MenuModel();
    }
    public function adduser()
    {
        echo "hello there";
    }
The models have been created and exist. When i run the code i get this error:
https://prnt.sc/3uXJTJ3ptcAv
Code:
Error

Class 'APP\Models\UsersModel' not found

APPPATH\Controllers\TestSqlite3.php at line 17

10 {
11    public $usersModel;
12    public $menuModel;
13    public function __construct()
14    {
15        // $this->usersModel = model("UsersModel");
16        // $this->menuModel = model("MenuModel");
17        $this->usersModel = new UsersModel();
18        $this->menuModel = new MenuModel();
19    }
20    public function adduser()
21    {
22        echo "hello there";
23    }
24 }
But when change the code for creating the model instance as per the following code, it seems to work ok:
Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;

// use APP\Models\MenuModel;
// use APP\Models\UsersModel;

class TestSqlite3 extends BaseController
{
    public $usersModel;
    public $menuModel;
    public function __construct()
    {
        $this->usersModel = model("UsersModel");
        $this->menuModel = model("MenuModel");
        // $this->usersModel = new UsersModel();
        // $this->menuModel = new MenuModel();
    }
    public function adduser()
    {
        echo "hello there";
    }


Appreciate any help. Thank you
Reply
#2

APP?
The namespace is translated to the path. If paths are case-sensitive on your operating system, then spelling the namespace correctly plays a huge role.
Reply
#3

(02-20-2022, 06:13 AM)iRedds Wrote: APP?
The namespace is translated to the path. If paths are case-sensitive on your operating system, then spelling the namespace correctly plays a huge role.

Hi,

You were right. I thought they were case insensitive. I corrected that and it works fine. Thank you so much.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB