{"record":{"id":"edefcbbadbc18e2c","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-edefcb","errorCode":null,"errorMessage":"A dependency injection container is required to access the services related to the ORM","messagePattern":"A dependency injection container is required to access the services related to the ORM","errorType":"exception","errorClass":"Phalcon\\Mvc\\Model\\Exceptions\\ManagerOrmServicesUnavailable","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Model/Transaction/Manager.zep","lineNumber":111,"sourceCode":"     * @var array\n     */\n    protected transactions = [];\n\n    /**\n     * Phalcon\\Mvc\\Model\\Transaction\\Manager constructor\n     *\n     * @param DiInterface|null container\n     */\n    public function __construct(<DiInterface> container = null)\n    {\n        if !container {\n            let container = Di::getDefault();\n        }\n\n        let this->container = container;\n\n        if unlikely typeof container != \"object\" {\n            throw new ManagerOrmServicesUnavailable();\n        }\n    }\n\n    /**\n     * Remove all the transactions from the manager\n     */\n    public function collectTransactions() -> void\n    {\n        var transactions;\n\n        let transactions = this->transactions;\n\n        for _ in transactions {\n            let this->number--;\n        }\n\n        let this->transactions = [];\n    }","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Model/Transaction/Manager.zep#L93-L129","documentation":"Phalcon\\Mvc\\Model\\Transaction\\Manager needs the DI container to resolve the 'db' service when creating transactions. Its constructor falls back to Di::getDefault(); if no container was passed and no default container has been set (Di::setDefault never called), it throws ManagerOrmServicesUnavailable immediately at construction time.","triggerScenarios":"Executing new Manager() in a fresh CLI script, unit test, or worker before any DI container exists; constructing the manager statically at bootstrap before the application registers its services; instantiating it in a context where the default container was cleared (Di::reset()).","commonSituations":"PHPUnit tests that build a Manager without the application bootstrap; long-running queue workers with their own minimal bootstrapping; library code that news up the manager instead of resolving 'transactionManager' from DI; calling Di::reset() between requests in tests.","solutions":["Pass the container explicitly: new Manager($di) where $di has the 'db' service registered","Call Di::setDefault($di) before any Manager instantiation if you rely on the default-container fallback","Register the manager as a DI service ('transactionManager' => Manager::class) so it is only constructed after the container is live","In tests, set up a minimal container with a 'db' service (or double) before creating the Manager"],"exampleFix":"// before\n$txManager = new \\Phalcon\\Mvc\\Model\\Transaction\\Manager(); // no default DI -> throws\n\n// after\n$di = new \\Phalcon\\Di\\Di();\n$di->set('db', fn() => new \\Phalcon\\Db\\Adapter\\Pdo\\Mysql($config->db->toArray()));\n$txManager = new \\Phalcon\\Mvc\\Model\\Transaction\\Manager($di);\n// or earlier: \\Phalcon\\Di\\Di::setDefault($di);","handlingStrategy":"validation","validationCode":"// Before constructing the manager standalone\nuse Phalcon\\Di\\Di;\n\n$di = Di::getDefault();\nif ($di === null) {\n    throw new RuntimeException('Set up the DI container (with a \"db\" service) before using transactions');\n}\n$txManager = new \\Phalcon\\Mvc\\Model\\Transaction\\Manager($di);","typeGuard":null,"tryCatchPattern":"try {\n    $txManager = new \\Phalcon\\Mvc\\Model\\Transaction\\Manager($di);\n} catch (\\Phalcon\\Mvc\\Model\\Exceptions\\ManagerOrmServicesUnavailable $e) {\n    // bootstrap the container and retry construction once\n    $di = bootstrapDi();\n    $txManager = new \\Phalcon\\Mvc\\Model\\Transaction\\Manager($di);\n}","preventionTips":["Always pass the container explicitly instead of relying on Di::getDefault()","Register 'transactionManager' as a DI service so wiring is guaranteed at resolution time","In tests, build a minimal container with a 'db' double before any Manager creation"],"tags":["phalcon","orm","dependency-injection","transactions","bootstrap"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}