Welcome Guest, Not a member yet? Register   Sign In
how to get method args in model __construct()?
#1

[eluser]starenka[/eluser]
Hi, im trying to log certain user actions. It's easy to log it w/ a helper function, but this seems rather dumb to me. I was thinking about using hook or extending model class, which triggers certain keywords within a model method name (f.e save, delete, update) and do the stuff automatically for me. The problem is that apart from controller/method/params (which are easy to capture) i need to store also data passed to the methods. I've been looking thru the sources/trying to debug things, but i just can't find way to get the arguments (f.e in Model __construct). It must be a way how to get them, right? I suppose the methods are called somehow and args are passed after routing etc.

Any clue? Thanks starenka

PS. I don't like the idea of hacking the DB layer classes either
#2

[eluser]Zack Kitzmiller[/eluser]
If you're talk about passing variables into a Model or Library when you load it, you do this.

$this->load->model('Party_model', $array_of_config);
#3

[eluser]starenka[/eluser]
[quote author="techneke" date="1269548268"]If you're talk about passing variables into a Model or Library when you load it, you do this.

$this->load->model('Party_model', $array_of_config);[/quote]

Well, not really. Lemme explain then:

Controller: $this->model->method(args)
Model __construct(): get_the_args_passed_to_method_somehow()
#4

[eluser]Zack Kitzmiller[/eluser]
That's not really how it works.

The constructor is called when the model object is instantiated. Which is done here: $this->load->model('model');

At that point the object is 'Constructed.' You'd be better off using an initialize() method or somesuch in your model. Take a look at the native CI Libraries to get an idea.
#5

[eluser]starenka[/eluser]
I know it doesn't work that way, i just wrote it like that, as you don't seem to get the idea right. There must be way how to hook some code on calling the model method, but i don't see it.
#6

[eluser]danmontgomery[/eluser]
You can get the arguments passed to a function with func_get_args()... Personally, I would probably make the functions you want to log private, and use the __call() magic method to log data before calling (php5 only, if that's an issue).

Code:
public function __call($method, $args) {
    if(in_array($method, array('save','delete','update'))) {
        log_message('debug', 'So and so called function ' . $method);
        return call_user_func_array(array($this, '_' . $method), $args)
    }

    // Invalid method
    show_404();
}

private function _add($arg1, $arg2) {
    // Do some things
}

private function _update($arg1, $arg2) {
    // Do some things
}

Code:
$this->my_model->update($arg1, $arg2);




Theme © iAndrew 2016 - Forum software by © MyBB