Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

how to work with the services from the models

Hello, gentlemen. Tell me please. I create servise 'Redis'. It work in controllers $this->redis->set(.....). All ok, but how I can to do that in models? I need safe any data in redis when user->save(); or anymodel->save();



35.8k
Accepted
answer
edited Aug '16

Models implement the Phalcon\Di\InjectionAwareInterface, so you can use $this->getDI() to retrieve the assigned DI. Also, since models do not inherit from Phalcon\Di\Injectable, you have to use it like this:

class MyModel {
    public function save() {
        $this->getDI()->get('redis')->func();
        // etc..
    }
}


3.7k

Thank you very much!!! It worked at its best !!! So I do without you? ))