Welcome Guest, Not a member yet? Register   Sign In
Using model - issue while using methods
#1

[eluser]Spir[/eluser]
Hi I have a weird issue.
I load a model then I use some methods from that model but it says the model is null...

I tought it's because of the name of my model but after checking the reserved names it's look ok.

Here is my code within a model that works fine :

Code:
$this->load->model('log');
$this->log->add($some_vars);
I have put some debug within the constructor. it's executed fine.

Example :
Code:
class Log extends Model {

    function Log(){
        parent::Model();
        
        echo 'hello yourself';
    }
does print. But when I want to use my methods it crash :
Quote:Undefined property: My_other_model::$log</p>
Call to a member function add() on a non-object
#2

[eluser]GSV Sleeper Service[/eluser]
looks like it's conflicting with the CI logging class, take a look at systems/codeigniter/Common.php
Code:
/**
* Error Logging Interface
*
* We use this as a simple mechanism to access the logging
* class and send messages to be logged.
*
* @access    public
* @return    void
*/
function log_message($level = 'error', $message, $php_error = FALSE)
{
    static $LOG;
    
    $config =& get_config();
    if ($config['log_threshold'] == 0)
    {
        return;
    }

    $LOG =& load_class('Log');
    $LOG->write_log($level, $message, $php_error);
}
#3

[eluser]Spir[/eluser]
I also tried this but it the same results :
Code:
$this->load->model('log','mylog');
$this->mylog->add($some_vars);

I also changed the name of the model to mylog for example and it doesn't work either... It's look like something is wrong in my code but it's a totally classic model.
#4

[eluser]Spir[/eluser]
Can I load a model in a model? Maybe not? maybe that the reason I have that issue...
#5

[eluser]Spir[/eluser]
it looks like it's not possible.
http://ellislab.com/forums/viewthread/49625/

I have load my model in the controller but it sucks because I want to log some CRUD event of my models so it does not have to be manage in controller. Maybe I should set it has a helper? But I feel not because it's not an helper. I manage data in my Log model. I want my models to use the model "log" so they can log in the database some events.
I guess I should just have my log model to be a model and the other model to inherit the Log model so I can use those method.
Do you guys think that would be a good idea?
#6

[eluser]Spir[/eluser]
So basically I would have :

Code:
class Log extends Model {

    function Log(){
        parent::Model();
    }

    ...

}

And a model that is tracked by logs :
Code:
class My_model extends Log {

    function My_model(){
        parent::Model();
    }

    ...

}
#7

[eluser]Spir[/eluser]
finally I did this :

in my model called My_model (previously). So remember this is the model that need to load my Log model :

Code:
class My_model extends Model {

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


    function add(){
        // adding something

        // then log the create
        $this->_log(array('add','my_model'));
    }

    function _log($ome_vars){
        $CI =& get_instance();
        $CI->load->model('log');
        $CI->log->add($ome_vars);
    }
}

NB: I'm not using "Log" as a name for my model anymore. I guess I would have some conflict.
#8

[eluser]Colin Williams[/eluser]
Right. The issue is that when you load the model in another model, you don't get an instance of that model available to your current model. That's why getting a reference to the CI object is necessary.

For instance, this should work:

Code:
class A_model extends Model {

   var $ci;

   function A_model()
   {
      parent::Model();
      $this->ci =& get_instance();
   }

   function some_method()
   {
      $this->load->model('other_model');
      $this->ci->other_model->other_method();
   }

}




Theme © iAndrew 2016 - Forum software by © MyBB