Codeignitor - An uncaught Exception was encountered - php

I am trying to fetch users by creating model and controller, I am just at initial stage of learning codeignitor, can anyone help?
controllers/Users.php:
<?php
class Users extends CI_Controller {
public function show(){
$this->load->model('user_model');
$result = $this->user_model->get_users();
foreach($result as $object){
echo $object->id;
}
}
}
?>
models/user_model.php:
<?php
class User_model extends CI_Model {
public function get_users(){
$this->db->get('users');
}
}
?>
Error:
An uncaught Exception was encountered
Type: RuntimeException
Message: Unable to locate the model you have specified: User_model
Filename: /home/sanadpjz/public_html/ci/system/core/Loader.php
Line Number: 314
Backtrace:
File: /home/sanadpjz/public_html/ci/application/controllers/Users.php
Line: 7
Function: model
File: /home/sanadpjz/public_html/ci/index.php
Line: 292
Function: require_once
URL vising:
example.com/ci/index.php/users/show
Database configured in config/config.php
Database table 'users' exists and has 1 record.

It is clear the loader is unable to find the model file and that is because you are naming the file wrongly. The file name should start with a capital letter as the class name, mentioned in CI3 documentation.
To fix your issue change your model file from
models/user_model.php
to
models/User_model.php.

Related

Unable to locate the model you have specified codeigniter 3

when i am working with localhost then everything is fine. But When i uploaded in hosting then its show error " Unable to locate the model you have specified"
An uncaught Exception was encountered Type: RuntimeException Message:
Unable to locate the model you have specified: Stock_model Filename:
/home/autorentbd/public_html/pharmacy/system/core/Loader.php Line
Number: 344
Backtrace:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Stock extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper("url");
$this->load->library("pagination");
$this->load->model('Stock/stock_model'); // My Model name is stock_model.php, is in module folder.
}
public function stock_list($data = NULL)
{
$data['title'] ='Medicine Stock List'; //data title
$data['stock_list'] = $this->stock_model->stock_list(); //call model function
$data['view_all'] = 'stock/stock_list'; //view file
$this->templates->version_one($data); //template
}
}
And My Model name: stock_model.php, My Class name is: Stock_model. all in under module folder.
Please check your file and class name, like if class name is Stock_model then load with "Stock_model" not stock_model. because some server are followed case sensitive.
CodeIgnitor 3 and further versions are case sensitive. You need to define controller as well as model name in first letter caps form. So you need to define your model as
$this->load->model('Stock/Stock_model');

Controller - Model in CodeIgniter and "Undefined property"

<?php
class Success_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//insert into user table
function get_all()
{
$query = $this->db->get('session'); // = select * from session
return $query->result();
}
}
<?php
class Success extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('success_model');
}
public function index()
{
$data= $this->Success_model->get_all();
$this->load->view('success_view', $data);
}
}
?>
As You can see this is good, but I have error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Success::$Success_model
Filename: controllers/success.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\session\application\controllers\success.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\session\index.php
Line: 292
Function: require_once
Fatal error: Call to a member function get_all() on null in C:\xampp\htdocs\session\application\controllers\success.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to a member function get_all() on null
Filename: controllers/success.php
Line Number: 15
Backtrace:
I'm looking, and looking, but I dont know what is bad. The good directories are files, other classes are working but this not.
Please help. I'm just tired, because after 2 hours searching mistakes...
For CI v3, afaik
I think I see it:
$data= $this->Success_model->get_all();
Should be:
$data = $this->success_model->get_all(); // Notice the lowercase success_model
PHP variables are case-sensitive where as functions and class names are not.
One more solution would be to change
$this->load->model('success_model');
into
$this->load->model('Success_model');
because then you can use
$data= $this->Success_model->get_all();

Unable to use include function a file in Codeigniter

A PHP Error was encountered
Severity: Warning
Message: include(../models/loginDao.php): failed to open stream: No such file or directory
Filename: controllers/LoginCon.php
Line Number: 3
Backtrace:
File: C:\wamp\www\xxx\CodeIgniter-3.0.0\application\controllers\LoginCon.php
Line: 3
Function: _error_handler
File: C:\wamp\www\xxx\CodeIgniter-3.0.0\application\controllers\LoginCon.php
Line: 3
Function: include
File: C:\wamp\www\xxx\CodeIgniter-3.0.0\index.php
Line: 292
Function: require_once
But file named loginDao.php existed, in that path specified.
Please help me out as I am new to CodeIgniter
The loading of codeigniter models & controllers, should only have first letter of class and file name upper case
Controller
CodeIgniter User Guide Controllers
File name: Logincon.php
<?php
class Logincon extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('logindao');
}
public function index() {
$data['something'] = $this->logindao->get_somthing();
$this->load->view('login', $data);
}
}
Model
CodeIgniter Userguide Models
File name: Logindao.php
<?php
class Logindao extends CI_Model {
public function get_somthing() {
// Some model code
}
}
Your include method: I would not load models this way.
<?php
include APPPATH . 'models/Logindao.php';
// Or
include FCPATH . `application/models/Logindao.php`;

Why ERROR in Codeigniter

I have error, please help
A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$user_model
Filename: controllers/user.php
Line Number: 15
Backtrace:
File: C:\wamp\www\apcodeigniter\application\controllers\user.php Line:
15 Function: _error_handler
File: C:\wamp\www\apcodeigniter\index.php Line: 292 Function:
require_once
Line 15 is: public function get()
public function get()
{
$this->user_model->get();
}
You haven't loaded the model yet.
Try changing the get method in User controller to -
public function get()
{
$this->load->model('user_model');
$this->user_model->get();
}
What I usually do in controllers, which are going to be depedent on that model, and each method would require some method of that model.
/*
* Load the model in the controller's constructor
*/
class User extends CI_Controller
{
function __construct()
{
parent::_construct(); //This is important if you're adding a constructor in the Controller, so that it can properly initialize the scope of CI_Controller in itselves
$this->load->model(array('user_model'));
}
public function get() //public here really isn't necessary, but kept it for understanding
{
$this->user_model->get();
}
}
Hope this helps.

load extended controller from CI spark

I have a CodeIgniter spark defining&autoloading a class libraries/Nci_nicecontroller.php:
class Nci_nicecontroller extends CI_Controller {
//...
}
//autoloaded using $autoload['libraries'][]='nci_nicecontroller';
now I want to use Nci_nicecontroller in my application's controller, e.g.:
class Welcome extends Nci_nicecontroller {
//...
}
//autoloaded using $autoload['sparks'][]='nci-extensions/0.0.4';
obviously, I can't just $this->load->spark in my controller's constructor, because it's required on class extension.
when not using sparks, I just put the Nci_nicecontroller in a core/MY_Controller.php, but with sparks, this won't work.
I tried autoloading the spark in my application, which didn't work.
Then I tried:
get_instance()->load->spark('nci-extensions/0.0.4');
in the header of controllers/welcome.php, which gives me following errors:
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/welcome.php
Line Number: 2
--and--
Fatal error: Call to a member function spark() on a non-object in
C:\xampp\htdocs\CodeIgniter_demo\application\controllers\welcome.php on line 2
what shall I do?
Try to do something like that:
class Wellcome extends CI_Controller {
protected static $_nci = null;
public function __construct(){
parent::__construct();
$this->_nci = $this->load->spark('nci-extensions/0.0.4');
}
public function index(){
$result = $this->_nci->getSomething();
echo $result;
}
}

Categories