Welcome Guest, Not a member yet? Register   Sign In
Extending CI_MODEL but nothing....
#1

[eluser]andreffonseca[/eluser]
Hi to all,

I got some kind of a problem in my code and i can't fix it.
It goes like this:

I create a model

Code:
class Fieldvalidation extends CI_Model{

function __construct() {
        parent::__construct();
        
    }

    /**
     *
     * @param array $_params
     * @return array
     */
    private function _validateFormFields($_params)
    {
        
        $r = $this->db->query("select * from users");
        print_r($r);
        
        exit(0);
        
    }
}

The problem is when i run this code it gives me a
Quote: Fatal error: Call to a member function query() on a non-object in (...)

but if i done this:
Code:
$CI = & get_instance();
        $r = $CI->db->query("select * from users");
        print_r($r);

everything works like a charm...

I must always use "get_instance()" to use loaded libraries? The extends isn't enought?

Using CI 2.1.3.

Thanks in Advance
#2

[eluser]andreffonseca[/eluser]
Someone please....got stuck in here....
#3

[eluser]boltsabre[/eluser]
how are you calling your model function _validateFormFields?

This is just a guess, but because it's a private method, if you try to call it direct from your controller you'll run into problems, however, if it was to be called from inside your model from another method it should work.

I'm at work atm so cannot test it, I'll leave it up to you, please report back if it now works or not when you call it from another method inside the same model.
#4

[eluser]andreffonseca[/eluser]
I call the _validateFormFields with a public function (not in this code) in this model.
Then i call the this public function from the controller.

It's my method of development... i never expose the function o actualy do the work. I create a public function and inside this public i call the private function for working ... Smile

Thanks
#5

[eluser]TheFuzzy0ne[/eluser]
Have you loaded your database?
Code:
function __construct()
{
    parent::__construct();
    $this->load->database();
}
#6

[eluser]andreffonseca[/eluser]
Hi TheFuzzy0ne,

Already try that but normally i use the autoload, and autoload the database library.
But the result is the same.

Thanks
#7

[eluser]TheFuzzy0ne[/eluser]
What version of PHP are you using? If it's really old, instead of:
Code:
function __construct()
{
    parent::__construct();
}

You may need to do this instead:
Code:
function Fieldvalidation()
{
    parent::Fieldvalidation();
}
#8

[eluser]andreffonseca[/eluser]
Hi TheFuzzy0ne and thanks again for replay.

I'm using php version 5.4.3.

Thanks in Advance
André Fonseca
#9

[eluser]boltsabre[/eluser]
Strange...
Try a new private test model method that just echo's something out... does that work? Do you have other private methods that already work?
#10

[eluser]andreffonseca[/eluser]
Hi boltsabre,

Everything is working fine. Is in the Model when i try to access any loaded libraries before i do the & get_instance it gives me that error.
I thing somehow the model class is not inherit the get_instance.



I think there is something wrong with the model library.
Code:
/*This code belongs to the model library from the CI code*/

class CI_Model {

/**
  * Constructor
  *
  * @access public
  */
function __construct()
{
  log_message('debug', "Model Class Initialized");
}

/**
  * __get
  *
  * Allows models to access CI's loaded classes using the same
  * syntax as controllers.
  *
  * @param string
  * @access private
  */
function __get($key)
{
  $CI =& get_instance();
  return $CI->$key;
}
}




Theme © iAndrew 2016 - Forum software by © MyBB