We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Using different database users per model for read and write?


I just started with Phalcon and was trying to use different users for read and wirte access per model.

I'm trying this by using a connection per user (2 per model) and set those for read/write connection service.

But when accessing Model::find I'm receiving the error: "Service 'db' wasn't found in the dependency injection container"

Am I doing something wrong? Probably an easier method I am missing?

Thanks for any advice.

Kind regards.


Hi Bastian, just set both connection services in the model's initializer:


class Robots extends Phalcon\Mvc\Model
    public function initialize()

Thanks for the reply.

That is what I'm doing, but for some reason I still have to set a connection as 'db' in the DI when trying to use Robots::find(...);

Is there some secret method or just some method I'm missing to call so that the standard 'db' connection is not needed?


Try to check what method is currently requesting that service, in public/index.php print the exception's backtrace:

try {
} catch(Exception $e) {
 echo $e->getException(), '<br>';
 echo $e->getTraceAsString();

Thanks, I just found the error.

It was because my Model extends a Base class and calls it's initialize function at the start of it's own. But the Base class uses skipAttributes, which is the called before the Model sets the read/write connection service.

Thanks for the help.