We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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();



77.7k
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..
    }
}


14.4k

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