on the server I can not load a model that is in another module in the HMVC, framework of the Codeigniter framework, I tried this way:
Class Contract extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Contract_model', 'contract');
$this->load->model('register/customer_model', 'customer');
$this->load->model('register/daydue_model', 'due');
}
}
Structure
In the localhost works quietly, since the server is giving this error::
Message: Unable to locate the model you have specified: Daydue_model
When you uploading on server use casesensitive it does not create problem on localhost. your model name is incorrect.
Do it like this
$this->load->model('register/daydue_model', 'due');
And rename your model name to Daydue_model and check the class name same is the name of model like:
class Daydue_model extends CI_Model
I am aware of the other questions on the platform for this issue however I am having a very unusual issue with this.
I have a model Company_Model.php which is being autoloaded in the autoload.php and the Class is built like this:
Class Company_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function foo()
{
echo 'bar';
}
}
However I am still getting this error when I load the page:
I am running on Apache 2, PHP 5.5.9 on Ubuntu 14.04, I cannot find an error log for this issue and now I am baffled, any help would be gratefully recieved.
I have check the uppercasing and all of the other tips from StackOverflow but still no joy.
Edit
Auto load code
$autoload['model'] = array('company_model');
When you use codeigniter 3 you have to make sure your class names and file names only have the first letter upper case as explained here
Class Naming Style Guide
Filename Style Guide
<?php
class Company_model extends CI_Model {
}
And file name then should be Company_model.php
$autoload['model'] = array('company_model');
If in sub folder
models > sub folder > Company_model.php
$autoload['model'] = array('subfolder/company_model');
If need to call it on controller only
$this->load->model('company_model');
$this->company_model->function();
Sub folder
$this->load->model('subfolder/company_model');
$this->company_model->function();
Make sure your class name and file name should be same and first Letter should be Capital
Example
class Login extends CI_Model{
//some code
}
above code, you can see the class Login first letter is capital.same name you have save of your model file name like Login.php in the model directory of your project folder
Try Changing the class name Company_Model to Company_model and also change your file name Company_Model.php to company_model.php
The browser:
Unable to locate the specified class: Session.php
This is my Controller:
<?php
class Chat extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Chat_model');
}
public function index() {
$this->view_data['chat_id'] = 1;
$this->view_data['student_id'] = $this->session->userdata('student_id');
$this->view_data['page_content'] = 'chat';
$this->load->view('chat');
}
public function ajax_addChatMessage() {
$chat_id = $this->input->post('chat_id');
$student_id = $this->input->post('student_id');
$bericht = $this->input->post('chat_id', TRUE);
$this->Chat_model->addChatMessage($chat_id, $student_id, $bericht);
}
}
When I put my model in comment in the parent::__construct(); // $this->load->model('Chat_model'); the error is gone.
This is my Chat_model:
<?php
class Chat_model extends CI_Controller {
public function Chat_model() {
parent::__construct();
}
public function addChatMessage($chat_id, $student_id, $bericht) {
$query = "INSERT INTO tbl_chatberichten (chat_id, student_id, bericht) VALUES (?,?,?)";
$this->db->query($query, array($chat_id, $student_id, $bericht));
}
}
class Chat_model extends CI_Controller
should be
class Chat_model extends CI_Model
If you use Codeigniter Modular Extensions HMVC this error can occur if you forget to change your class to extend MX_Controller instead of CI_Controller
So in your case you would start your class with:
class Chat extends MX_Controller {}
Instead of:
class Chat extends CI_Controller {}
I get the same error message when I involve the PDF library with the class name Pdf.php and load it via autoload as 'pdf'.
My mistake, the controller that will display my page I also named Pdf.php, and the error message appears ( that's the reason why I found this question :) ). The problem was immediately solved after I replaced the name of my controller with another name.
Add changes into your library configurations in application/config/autoload.php file
$autoload['libraries'] = array('database', 'session');
and in application/config/config.php set the encryption key(any key u like)
$config['encryption_key'] = 'thu23456789#[n,';
If you still get same error then copy
System/library/Session/Session.php to System/library/ folder, then it
should work
My solution:
Preface: If you don't use hooks, this will not be the solution for you.
I had this same issue, after upgrading to v3.0.6, and I definitely had everything setup correctly, as this is an existing site just being upgraded to v3+. My issue boiled down to hooks that I had loading 'pre-controller'. My hooks worked with v 2.0.X of CodeIgniter, but not with v3+.
If you are loading hooks before the session class is loaded, and your hook has a dependency on the session class, that may be your culprit. Try changing any pre-controller hooks to post-controller, or commenting them out completely to see if that fixes your issue. This is all located in application/config/hooks.php.
I encountered with same error. I attached "session" on --->
yourproject/application/config/autoload.php
$autoload['drivers'] = array("session");
Change your load library configuration in application/config/autoload.php file
$autoload['libraries'] = array('database', 'session');
In application/config/config.php set the encryption key(any key u like)
$config['encryption_key'] = 'thu23456789#[n,';
In my case the library name and controller class name was same i.e my controller class name was Ajaxer and also my library class name was Ajaxer. I changed my library class name to Ajaxer_lib and its resolved the issue.
In your Chat_model, try to change class Chat_model extends CI_Controller to class Chat_model extends CI_Model.
If none of the above solution worked, my workaround was:
In my autoload config, I had a library called 'auth'.
however, in my custom controller, somehow it was being messed up because 'auth' wasn't being auto-loaded(I've checked everything). Since I am only using it when logging in, I removed it from my autoload config and load it in my custom controller.
I never had this issue before but suddenly one day it happened.
Using [CodeIgniter v3]
Add this instead of your Chat controller constructor
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Chat_model');
}
This loads the session library to your controller so that you can use the methods.
Model Extends CI_Model Not CI_Controller
class Chat_model extends CI_Model {
.............
}
Modify your library configurations in application/config/autoload.php file
$autoload['libraries'] = array('database','ci_session');
Set the encryption key for your applicatiion in application/config/config.php
$config['encryption_key'] = 'sdjfkdjkfj';
Copy system/libararies/Session/Session.php to application/libraries/
Rename application/libraries/Session.php to CI_Session.php
Now, CI_Session object will be available through below line
$this->ci_session
Very frustrating -
When I'm developing locally everything works perfect . I then started testing on my host (GoDaddy) for the first time and I get the error
Fatal error: Class 'MY_Model' not found in /home/content/49/12220009/html/application/models/user_model.php on line 3
The code is very basic -
User_model.php
class User_model extends MY_Model {
protected $_table;
public function __construct() {
parent::__construct();
$this->_table = "user";
}
MY_Model.php (application/core/My_Model.php)
class MY_Model extends CI_Model
{
public function __construct() {
echo "in my model ctor";
parent::__construct();
}
And it all starts with a controller loading User_model as expected -
$this->load->model('user_model');
Any thoughts on why this would work locally but not on my host , or how to solve it in other words?
it is definitely a case-sensitivity issue; what you should do is follow these two steps
1.follow these conventions and you are good to go
class Model_name extends CI_Model
Model_name is the name of your class... Class names must have the first letter capitalized with the rest of the name lowercase
and then the file name should be in lower case like this
application/models/model_name.php
This also happened to me:
on Linux system ,it show the error you specify while on window it was fine.
this is because Linux is case sensitivity .
what i did?;
I rename the file from /core/MY_model to /core/MY_Model.
and ,then it solved my problem .
CodeIgniter 3 solution
The class name and the class filename must have an uppercase first character.
Starting with CodeIgniter 3.0, all class filenames (libraries,
drivers, controllers and models) must be named in a Ucfirst-like
manner or in other words - they must start with a capital letter.
For example, if you have the following library file:
application/libraries/mylibrary.php
… then you’ll have to rename it to:
application/libraries/Mylibrary.php
i have written a small codeigniter test app that is currently running on my windows box.
i created a linux vm and have tried to install the app on this new virtual server.
some of my web app is running properly but other parts, no.
specifically, this works:
http://123.123.123.123/myapp/controller1/
but this does not:
http://123.123.123.123/myapp/controller2/mymethod/1/2/3
It fails with an error that it can't load controller2_model.
Here's the actual code for the controller that is failing (it's really called xferLogger vs. controller2):
class xferLogger extends CI_Controller {
public function __construct() {
parent::__construct();
echo(2);
$this->load->model('xferLogger_model');
$this->load->helper('date'); //this library is needed for the base_url() method that is being called in the view "result.php"
$this->load->helper('url');
}
and here's the model:
class xferLogger_model extends CI_Model {
public function __construct() {
$this->load->database();
}
The full error message is: An error was encountered. Unable to locate the model you have specified: xferlogger_model.
Here's something I noticed. in the error message, you'll notice that the "L" in logger is lowercase. but in my code, it's a capital L.
I've checked in my controller, the model itself, and also the routes.php file. I can't seem to find any problems with my casing.
??
From the userguide: Class names must have the first letter capitalized with the rest of the name lowercase. So therefore:
class Xferlogger_model extends CI_Model // First letter capitalised
and your model load
$this->load->model('xferlogger_model'); // lower case
and your PHP filename
xferlogger_model.php // lower case
Codeigniter Model Userguide