{"record":{"id":"083ad25a50cb04d7","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-083ad2","errorCode":null,"errorMessage":"A dependency injection container is required to access internal services","messagePattern":"A dependency injection container is required to access internal services","errorType":"exception","errorClass":"Phalcon\\Mvc\\Model\\MetaData\\Exceptions\\ContainerRequired","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Model/MetaData.zep","lineNumber":348,"sourceCode":"        if false === isset(this->columnMap[key]) {\n            if false === this->initializeColumnMap(model, key) {\n                return null;\n            }\n        }\n        return key;\n    }\n\n    /**\n     * Returns the DependencyInjector container\n     */\n    public function getDI() -> <DiInterface>\n    {\n        var container;\n\n        let container = <DiInterface> this->container;\n\n        if typeof container != \"object\" {\n            throw new ContainerRequired();\n        }\n\n        return container;\n    }\n\n    /**\n     * Returns attributes and their data types\n     *\n     *```php\n     * print_r(\n     *     $metaData->getDataTypes(\n     *         new Invoices()\n     *     )\n     * );\n     *```\n     */\n    public function getDataTypes(<ModelInterface> model) -> array\n    {","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Model/MetaData.zep#L330-L366","documentation":"Phalcon\\Mvc\\Model\\MetaData\\MetaData::getDI() returns the DI container attached to the metadata component and throws this exception when none was set. The metadata pipeline needs the container on every cache miss: initializeMetaData() and initializeColumnMap() call getDI() to hand the extraction strategy (Introspection or Annotations) the container it uses to reach the database and the annotations service. So it surfaces not only on direct getDI() calls but on the first metadata computation for any model.","triggerScenarios":"Constructing a MetaData adapter manually (new MetaData\\Memory(), new MetaData\\Redis([...])) and calling a getter such as getAttributes($model) without setDI(); a custom 'modelsMetadata' service whose factory forgets the container; model usage after the default DI was cleared in a long-running worker.","commonSituations":"Standalone scripts or PHPUnit harnesses that instantiate metadata adapters directly; refactored bootstrap code that stops resolving modelsMetadata from the DI; workers using Di::getDefault() after it was reset to null.","solutions":["Call $metaData->setDI($di) right after constructing the adapter.","Prefer resolving the adapter as the shared 'modelsMetadata' service in the DI — Phalcon injects the container automatically.","Ensure a default container exists (Di::setDefault) before any model operation in CLI and worker entry points."],"exampleFix":"// before\n$metaData = new Phalcon\\Mvc\\Model\\MetaData\\Memory();\n$metaData->getAttributes(new Invoices()); // ContainerRequired on first cache miss\n\n// after\n$di = Phalcon\\Di\\Di::getDefault() ?? new Phalcon\\Di\\FactoryDefault();\n$metaData = new Phalcon\\Mvc\\Model\\MetaData\\Memory();\n$metaData->setDI($di);\n$metaData->getAttributes(new Invoices());","handlingStrategy":"validation","validationCode":"// Precondition: attach a container before any metadata call\n$di = Phalcon\\Di\\Di::getDefault() ?? new Phalcon\\Di\\FactoryDefault();\n$metaData->setDI($di);","typeGuard":"function metaDataHasDi(Phalcon\\Mvc\\Model\\MetaData $metaData): bool\n{\n    try {\n        return $metaData->getDI() instanceof \\Phalcon\\Di\\DiInterface;\n    } catch (\\Phalcon\\Mvc\\Model\\MetaData\\Exceptions\\ContainerRequired $e) {\n        return false;\n    }\n}","tryCatchPattern":"use Phalcon\\Mvc\\Model\\MetaData\\Exceptions\\ContainerRequired;\n\ntry {\n    $metaData->getAttributes($model);\n} catch (ContainerRequired $e) {\n    $metaData->setDI(Phalcon\\Di\\Di::getDefault());\n    $metaData->getAttributes($model);\n}","preventionTips":["Always resolve metadata adapters via the shared 'modelsMetadata' DI service.","Make setDI() part of the adapter fixture in tests.","Never share one MetaData adapter between processes with different DI containers."],"tags":["phalcon","orm","metadata","dependency-injection","bootstrap"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}