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.

Use PHQL from an external class (out of Controllers)

Hello folk! I'm trying to run a PHQL from an external class, but I got the following error:

A dependency injection object is required to access ORM services

This is my code:

$modelsManager = new Manager();
        $res = $modelsManager->executeQuery(
            'name' => $name,
            'age'     => $age,
            'color'   => $color

At the top, I have included this:

use Phalcon\Di;
use Phalcon\Mvc\Model\Manager;

What is wrong? Must I use Micro? I don't know how to make it work.

Related thread:

edited 19d ago

And where you have this code? You need to access manager for di(modelsManager service) or set Di for this new manager.


Thanks for answering!

This external class is placed in /app/somewhere/External.php.

Yeah, that's I want: access to manager, but I'm a bit new at MVC, so I don't get it quickly. Sorry.

Could you tell me how can I access it?

You need to access DI from your appliaction obviously. But framework should acces it itself. If it doesn't then it means that you don't have DI in your app at all so im not sure what are you doing. Just make this class extends Injectable and just do $this->modelsManager


Hmm, now put extends Injectable in my Class and I got this error:

Fatal error: Class 'Injectable' not found

This file is outside /app/controller/ is that the reason? is isolated.

edited 17d ago

You need to add use keyword with proper namespace obviously. What IDE you use? It should inform you about it.

I think it's obvious i meant Phalcon\Di\Injectable


I use Geany as my editor