CodeIgniter Forums
Undefined property: Database::$db in model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Undefined property: Database::$db in model (/showthread.php?tid=64634)



Undefined property: Database::$db in model - lcwakeman - 03-15-2016

And now for my next issue. First, I would like to point out that this (before updating) has been working on CI 2.

I am getting the above error executing a query builder query in a model.

Controller code:

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Database extends CI_Controller {

/**
* Contructor
*/
 public function __construct()
 {
   parent::__construct();
 }
 
/**
* Index - Root function
*/
    public function index()
    {
        $this->load->view('welcome_message');
    }
    
/**
* Version - display schema version
*/
    public function version()
    {
        $this->load->model('Crud_m');
        $this->Crud_m->setTable('schema_version');
        echo $this->Crud_m->getTable().'<br>';
echo '<pre>'; // debug
print_r ($this->Crud_m->get()); // debug
echo '</pre>'; // debug
    }
    
}
and the model code:

<?php
class Crud_m extends CI_model
{
    private $s_table;
    
/**
 * Contructor
*/
    function __construct ()
    {
        parent::__construct();
    }

/**
 *    getTable - returns the table we are dealing with
 */
    function getTable()
    {
        return $this->s_table;
    }

/**
 *    setTable - sets the table we are dealing with
 */
    function setTable($s_table)
    {
        $this->s_table = $s_table;
        return $this->s_table;
    }

/**
 * get - Retrieves a record
*/
    function get($a_where = '')
    {
        $a_data = Array();
        $o_query = $this->db->get_where($this->s_table, $a_where);
        if ($o_query->num_rows() == 1)
        {
            $a_data = $o_query->row_array();
        } else if ($o_query->num_rows() > 1)
    {
            $a_data = $o_query->result_array();
    }
        $o_query->free_result();
        return $a_data;
    }
    
}

Code:
<?php
class Crud_m extends CI_model
{
    private $s_table;
    
/**
* Contructor
*/
    function __construct ()
    {
        parent::__construct();
    }

/**
*    getTable - returns the table we are dealing with
*/
    function getTable()
    {
        return $this->s_table;
    }

/**
*    setTable - sets the table we are dealing with
*/
    function setTable($s_table)
    {
        $this->s_table = $s_table;
        return $this->s_table;
    }

/**
* get - Retrieves a record
*/
    function get($a_where = '')
    {
        $a_data = Array();
        $o_query = $this->db->get_where($this->s_table, $a_where);
        if ($o_query->num_rows() == 1)
        {
            $a_data = $o_query->row_array();
        } else if ($o_query->num_rows() > 1)
   {
            $a_data = $o_query->result_array();
   }
        $o_query->free_result();
        return $a_data;
    }
    
}

and the result:

Quote:schema_version



A PHP Error was encountered



Severity: Notice


Message:  Undefined property: Database::$db


Filename: core/Model.php


Line Number: 77




Backtrace:











File: C:\Users\larry\Projects\BGA Media\Great Oak\www\application\models\Crud_m.php

Line: 58

Function: __get







File: C:\Users\larry\Projects\BGA Media\Great Oak\www\application\controllers\management\Database.php

Line: 41

Function: get











File: C:\Users\larry\Projects\BGA Media\Great Oak\www\index.php

Line: 292

Function: require_once








Fatal error:  Call to a member function get_where() on a non-object in C:\Users\larry\Projects\BGA Media\Great Oak\www\application\models\Crud_m.php on line 58






A PHP Error was encountered



Severity: Error


Message:  Call to a member function get_where() on a non-object


Filename: models/Crud_m.php


Line Number: 58




Backtrace:

I looked at core/model.php and it isn't extending any thing so other than the attributes and methods I define in the model, it is empty and there is now db object. Huh


RE: Undefined property: Database::$db in model - Narf - 03-15-2016

This means you haven't loaded the database class, which happens either via a $this->load->database() class or via the config/autoload.php file.


RE: Undefined property: Database::$db in model - lcwakeman - 03-15-2016

It has been a while since I started a new instance. Thanks but it would be nice if that were mentioned in the documentation on models. I think the autoload of the database object was the default in CI 2.0 but that was years ago.


RE: Undefined property: Database::$db in model - Narf - 03-15-2016

(03-15-2016, 12:51 PM)lcwakeman Wrote: It has been a while since I started a new instance. Thanks but it would be nice if that were mentioned in the documentation on models. I think the autoload of the database object was the default in CI 2.0 but that was years ago.

... it has always worked like that and it is properly documented.