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.
Related
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.
modules
--customers
----controllers
-------customers.php
----models
-------customers_model.php
-------handler_model.php
Hi im trying to load two models in my controllers by using this:
class Customers extends CoreAPI
{
public function __construct()
{
parent :: __construct();
$this->load->model('Customers_model', 'Customers');
$this->load->model('Customers/Handler_model');
}
public function test_get()
{
$this->Handler_model->test;
exit;
}
However, I always get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$test
Filename: core/Model.php
Line Number: 73
I tried the two different ways of loading a model as stated.
Folder Structure
Please help. Thank you.
class Handler_model extends CI_Model
{
public function __construct()
{
parent :: __construct();
}
public function test()
{
echo 'TEST';
}
}
<?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();
controller
public function add_package()
{
$this->load->model('admin_model');
$this->Admin_model->add_packages($package);
}
admin_model.php
class Admin_model extends CI_Model
{
public function add_packages($data)
{
return $this->db->insert('george_packges',$data);
}
}
Why i will getting this error Undefined property: Admin::$Admin_model and also this
fatal error: Call to a member function add_packages() on null in
C:\xampp\htdocs\holiday\application\controllers\Admin.php on line 130
Change the loading model variable as the model loaded and used was different so the variable was not matching.
$this->Admin_model->add_packages($package);//currently used
$this->admin_model->add_packages($package);
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;
}
}