Welcome Guest, Not a member yet? Register   Sign In
Database object not available to Model(s)
#1

[eluser]conord[/eluser]
EDIT: As suspected this was user-error. I had inserted some code in the system/core/model.php code that overwrote the default object. Removed code and database access has been restored. Leaving thread here in case same happens to someone else.


I'm still learning the ins-and-outs with CI and was working with models. Initially, I created a model and called it from a controller - but not the matching controller (eg: controller was Users and model was Stores). When I tried a DB query in the Stores model, got the "calling member function on non-object" error. BTW, the database library should be auto-loaded via the config.

I figured this was due to where I called the model from, so I added $CI->get_instance(); and $this->db = $CI->load('database'); Everything fixed.

Recently though, I created a base model (My_Model.php) which extends CI_Model. I then extend MY_Model in my Users model. Each class calls parent::_constructor() so I thought I should have access to the full CI instance through $this. That doesn't seem to be the case.

Code:

Code:
class MY_Model extends CI_Model
{
    private $_errors = array();
    
    public function __construct()
    {
        parent::__construct();
    }
..snip...

}

Code:
<?php

class Usersmodel extends MY_Model
{
    private $_error;
    private $_user_table = "users";


    public function __construct()
    {
        parent::__construct();
    }
public function get_all_users($order_by = "username ASC")
    {
        $this->db->order_by($order_by);
        if (false === $result = $this->db->get())
        {
            $this->set_error();
            return false;
        }
        
        return $result->result_array();
    }
..snip..
}

The error is happening at the $this->db->order_by() line.

TLBig GrinR: Don't have access to $this->db inside my models.[/b]
#2

[eluser]pbflash[/eluser]
Make sure the database is in the autoload file. Also, you aren't setting a query. You need to tell it what you want to select and from what table. Just setting an order by is not going to give you any results.
#3

[eluser]CroNiX[/eluser]
After you make sure the database is loaded...
Code:
public function get_all_users($order_by = "username ASC")
{
  $data = $this->db
    ->select('field1, field2') //need to select something first.  If omitted it will SELECT(*)
    ->order_by($order_by)
    ->get('name_of_your_table'); //need a table name to get data from

  if ($data->num_rows() > 0)  //if we got some results
  {
    //return them as array
    return $data->result_array();
  }
  else
  {
    //set error and return FALSE
    $this->set_error();
    return FALSE;
  }      
}
#4

[eluser]conord[/eluser]
Thanks for the heads up on the query. I fixed that problem and confirmed (via logfile) that the database driver is being loaded. When I do print_r on $this inside the usersmodel class (in the get_all_users() method), here is the result:

Code:
Usersmodel Object
(
    [_error:Usersmodel:private] =>
    [_user_table:Usersmodel:private] => users
    [_access_table:Usersmodel:private] => functions_access
    [_errors:MY_Model:private] => Array
        (
        )

    [config] =>
    [db] =>
    [email] =>
    [form_validation] =>
    [input] =>
    [load] =>
    [router] =>
    [session] =>
    [table] =>
    [unit] =>
    [uri] =>
    [pagination] =>
)

New get_all_users w/ fixed query:
Code:
public function get_all_users($order_by = "username ASC")
    {
        if (false === $result = $this->db->select('*')->from($this->_user_table)->order_by($order_by)->get())
        {
            $this->set_error();
            return false;
        }
        
        return $result->result_array();
    }
I've stepped back through the code and as far as I can tell, all the classes are extending the appropriate parent class, and everyone calls parent::__constructor().

Am I correct in assuming that if I'm calling a Model from a Controller, that $this should contain the full $CI instance?
#5

[eluser]CroNiX[/eluser]
Yes, $this would be the full CI instance IN THE CONTROLLER, but not within the model.

Did you try it the exact way I wrote mine (with the exception of the select)? You changed yours a bit and they're not quite the same thing.
#6

[eluser]InsiteFX[/eluser]
Did you place your MY_Model in ./application/core/MY_Model.php ?

If the Class you are extending is in ./system/core then you place it in ./application/core
If the Class you are extending is in ./system/libraries then you place it in ./application/libraries
#7

[eluser]conord[/eluser]
Firstly, thanks for the responses. I'm having a little trouble wrapping my mind around this one.

To answer the questions: Yes, I copy/pasted the query and re-ran it and had the same result. The model method is being called from a controller. and Yes, the MY_Model is in the core folder and is being instantiated correctly.

I was able to get the code to work by using get_instance() in the base model, but that seems kludgy to me. But if that's the way to go, that's fine. I guess I'm just not understanding why the DB access is not available to a model, if called via a controller.
#8

[eluser]Aken[/eluser]
Did you write your own __get() method in your MY_Model ?
#9

[eluser]meigwilym[/eluser]
Also, is your MY_Model extending CI_Model (and not Model)?

Mei
#10

[eluser]conord[/eluser]
No to the __get() method in MY_Model and yes to extending CI_Model. I've also not modified any of the system files (except adding some var's for eclipse auto-complete).




Theme © iAndrew 2016 - Forum software by © MyBB