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';
}
}
Related
I am a newbie to Codeigniter and trying to build a service registration page. Not just the page but the whole website has this error.
$autoload['model'] = array('User_model','Material_model');
$autoload['libraries'] = array('database','form_validation');
class services extends CI_Controller {
public function service1()
{
$this->form_validation->set_rules('type','Type',trim|required);
$this->form_validation->set_rules('area','Area',trim|required);
$this->form_validation->set_rules('quantity','Quantity',trim|required);
$this->form_validation->set_rules('details','Details',trim|required);
$this->form_validation->set_rules('name','Name',trim|required);
$this->form_validation->set_rules('number','Number',trim|required);
$this->form_validation->set_rules('location','Location',trim|required);
if($this->form_validation->run()==FALSE){
$data['main_view']="page_view";
$this->load->view('layouts/main',$data);
} else{
$this->user_model->create_user();
redirect('/');
}
I'm getting this error in user model from material model. Th
Actually, you should load the model in your parent construct.
For example:
class services extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_directory_model, 'your_initialize_model');
}
}
And then, if you want to use your model. you can build code like i write below in your method.
$this->your_initialize_model->your_model_method();
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 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.
I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Cart::$load
Filename: controllers/cart.php
Line Number: 7
Fatal error: Call to a member function model() on a non-object in D:\xampp\htdocs\ci\application\controllers\cart.php on line 7
Can someone please tell me why it is not working?
The name of my controller is cart.php
<?php
class Cart extends CI_Controller {
public function Cart()
{
//parent::CI_Controller(); // We define the the Controller class is the parent.
$this->load->model("cart_model"); // Load our cart model for our entire class
}
public function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
}
?>
and my model is cart_model.php
<?php
class Cart_model extends CI_Model{
//public function _construct(){
//parent::_construct();
//}
public function retive_products(){
$query = $this->db->get("products");
return $query->result_array();
}
}
/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */
?>
Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.
Change the class constructor:
<?php
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
}
instead of
public function cart()
{
parent::CI_Controller();
Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .
Controller
public function index() {
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
Model
public function retive_products() {
$query = $this->db->get("products");
return $query->result_array();
}
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;
}
}