CodeIgniter: Undefined property $db - php

In CodeIgniter 3.1.3 I extended CI_Model in application/core/MY_Model.php:
class MY_Model extends CI_Model {
public function __construct($id = NULL) {
parent::__construct();
$this->load($id);
}
public function load($id) {
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->query($this->_table);
if ($row = $query->result()) {
// #todo Process results
}
// Free the resources.
$query->free_result();
}
}
My User_model looks like this:
class User_model extends MY_Model {
public function __construct($id = NULL) {
parent::__construct($id);
}
}
I also extended the CI_Controller in application/core/MY_Controller as follows:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
}
}
I autoloaded the database connection in application/config/autoload.php as:
$autoload['libraries'] = array('database');
Without loading the User_model in the controller I was able to run migrations, so the database connection is configured correctly. But when I added $this->load->model('User_model') I get the error "Undefined property: User_model::$db".
If I let User_model extend CI_Model it runs without errors and with a var_dump in the homepage's controller it shows that the database is autoloaded correctly. But as soon as I put MY_Model in between, the database class is undefined in the model and also $this->load in the model returns NULL, so it appears the model is not properly constructed.
I can only imagine this to be a very small mistake, but I've been staring at it for hours with several breaks in between and I just don't see it. Can anyone else help me?

From a controller, lets say A_controller
$this->load->library('MyLib');
In MyLib, my original issue is this block
$ci = &get_instance();
$ci->load->model('my_model');
$this->active_model = $ci->my_model;
Then the miracle come when it become
$ci = &get_instance();
$ci->load->model('my_model');
$ci->my_model->db = & $ci->db;
$this->basic_model = $ci->my_model;
When CI loads a model $db property come along with CI.
So, when you call $this->db in model which means you want "my_model" have the property, right?
What I did just link the property from CI to my_model!!!

Related

How to load a model in a master controller on codeigniter framework?

I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().

Codeigniter Error Unable to locate the specified class: Loader.php

i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

What is wrong with this CodeIgniter code?

I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');

Access an array in all views in codeigniter

I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.

error calling member function of alleged non-object in Codeigniter controller

I get the error
Fatal error: Call to a member function retrieve_products() on a non-object
The controller is:
<?php
class Cart extends CI_Controller { // Our Cart class extends the Controller class
public function _construct()
{
parent::_construct(); // We define the the Controller class is the parent.
$this->load->model('Cart_model'); // Load our cart model for our entire class
}
function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
}
}
The model is:
<?php
class Cart_model extends CI_Model {
function retrieve_products(){
$query = $this->db->get('products'); // Select the table products
return $query->result_array(); // Return the results in a array.
}
}
I want to say that your call
$data['products'] = $this->cart_model->retrieve_products();
Should be:
$data['products'] = $this->Cart_model->retrieve_products();
Ie: uppercase "C" in cart_model
Maybe we're using different versions (I have 1.7.2), but to declare a model, CI_ does not appear. My working code has the equivalent of:
class Cart_model extends Model
Also, the class should capitalized:
$this->Cart_model->retrieve_products();
(instead of)
$this->cart_model->retrieve_products();
I think its your typo error you have spelled construct function as _construct rather than __construct thats why codeigniter considers it as a function rather than a class constructor and model loading is limited to only that function.

Categories