Welcome Guest, Not a member yet? Register   Sign In
jamierumbelow / codeigniter-base-model fatal error
#1

[eluser]Jan_1[/eluser]
Does anyone have a "Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\codeigniter\application\core\MY_Model.php on line 216" ?

Where does that come from? Everything fresh installed...

Code:
<?php
class Test extends CI_Controller {

function __construct()
    {
        parent::__construct();  
        $this->load->model(array('test_model'));            
    }

public function index()
{
        $data['title']  =   'test';
        $data['view']   =   'test_view';
        $data['test']   =   $this->test_model->get_all();  
        $this->load->view('template', $data);
}
}
?>
Code:
<?php
class Test_model extends MY_Model { }
?>
Code:
/**
     * Fetch all the records in the table. Can be used as a generic call
     * to $this->_database->get() with scoped methods.
     */
    public function get_all()
    {
        $this->trigger('before_get');

        if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE)
        {
            $this->_database->where($this->soft_delete_key, FALSE);
        }

     $result = $this->_database->get($this->_table)           // <-- line 216
                           ->{$this->_return_type(1)}();
        $this->_temporary_return_type = $this->return_type;

        foreach ($result as $key => &$row)
        {
            $row = $this->trigger('after_get', $row, ($key == count($result) - 1));
        }

        $this->_with = array();
        return $result;
    }

TABLE `tests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`content` mediumtext NOT NULL,
`deleted` smallint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `tests` (`id`, `name`, `content`, `deleted`) VALUES
(1, 'Headline', 'Just an example!', 0);
#2

[eluser]Aken[/eluser]
A recent pull request broke the model. Here's my fix for it.
#3

[eluser]Jan_1[/eluser]
Thanks a lot!

Code:
/* --------------------------------------------------------------
     * MULTIPLE DB LOADING
     * ------------------------------------------------------------ */

    private function _set_database()
    {
        if (!$this->_db)
        {
            $this->_database = &$this->db;
        }
        else
        {
            $db_key = md5((is_string($this->_db)) ? $this->_db : $this->_db['database']);
            if(!isset($this->{$db_key}))
                {
                    $this->{$db_key} = $this->load->database($this->_db, TRUE);
                    $this->_database = &$this->{$db_key};
                }
            elseif (is_object($this->{$db_key}) && is_subclass_of(get_class($this->{$db_key}), 'CI_DB') )
                {
                    $this->_database = &$this->{$db_key};
                }
            else
                {
                    $this->_database = $this->load->database($this->_db, TRUE);
                }
        }
    }
#4

[eluser]Aken[/eluser]
That wasn't my code, but if it works for you, whatever.




Theme © iAndrew 2016 - Forum software by © MyBB