{"record":{"id":"47d8e71370282fb8","repo":"phalcon/cphalcon","slug":"invalid-injected-connection-service","errorCode":null,"errorMessage":"Invalid injected connection service","messagePattern":"Invalid injected connection service","errorType":"exception","errorClass":"Phalcon\\Mvc\\Model\\Exceptions\\InvalidConnectionService","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Model/Manager.zep","lineNumber":2491,"sourceCode":"        array connectionServices\n    ) -> <AdapterInterface> {\n        var container, service, connection;\n\n        let service = this->getConnectionService(model, connectionServices);\n\n        let container = <DiInterface> this->container;\n\n        if unlikely typeof container != \"object\" {\n            throw new ManagerOrmServicesUnavailable();\n        }\n\n        /**\n         * Request the connection service from the DI\n         */\n        let connection = <AdapterInterface> container->getShared(service);\n\n        if unlikely typeof connection != \"object\" {\n            throw new InvalidConnectionService();\n        }\n\n        return connection;\n    }\n\n    /**\n     * @param string $collection\n     * @param string $modelName\n     * @param string $modelRelation\n     *\n     * @return bool\n     */\n    private function checkHasRelationship(\n        string collection,\n         string modelName,\n         string modelRelation\n    ) -> bool {\n        var entityName;","sourceCodeStart":2473,"sourceCodeEnd":2509,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Model/Manager.zep#L2473-L2509","documentation":"Thrown by Manager::getConnection() after the DI container resolved the model's connection service to something that is not an object. The service name comes from the model's getConnectionService() (default 'db') or from the manager's per-model connectionServices overrides; container->getShared(service) must return an object (a Phalcon\\Db\\Adapter\\AdapterInterface). When the registered service returns null, a string, an array or any scalar, the connection lookup is aborted with this exception.","triggerScenarios":"A DI service registered under the model's connection service name that returns a non-object: a closure returning a config array, a service defined as raw array settings, a factory whose error path returns null, or $model->setConnectionService('databse') (typo) resolving a service that exists but is not a connection object.","commonSituations":"Registering 'db' as an array of connection parameters instead of a factory returning an Adapter; multi-database setups where one of the named connections was never registered; typos in setConnectionService()/setReadConnectionService()/setWriteConnectionService(); test doubles for the DI that return strings from getShared().","solutions":["Inspect $di->getService($model->getConnectionService()) and change its definition so every path returns a Phalcon\\Db\\Adapter\\AdapterInterface instance.","Verify the name: call $model->getConnectionService() (or getReadConnectionService()/getWriteConnectionService()) and confirm a service with exactly that name is registered.","If a factory closure returns conditionally, make sure no branch returns null or an array.","For multi-database apps, register every connection name any model references."],"exampleFix":"// before\n$di->set('db', function () {\n    return $this->getConfig()->database->toArray(); // array, not an adapter\n});\n$manager->getConnection($invoice); // InvalidConnectionService\n\n// after\n$di->set('db', function () {\n    $cfg = $this->getConfig()->database;\n    return new Phalcon\\Db\\Adapter\\Pdo\\Mysql([\n        'host'     => $cfg->host,\n        'username' => $cfg->username,\n        'password' => $cfg->password,\n        'dbname'   => $cfg->dbname,\n    ]);\n});","handlingStrategy":"type-guard","validationCode":"$service = $model->getConnectionService();\nif (!$di->has($service) || !is_object($di->getShared($service))) {\n    throw new RuntimeException('Connection service \"' . $service . '\" does not resolve to an object');\n}","typeGuard":"use Phalcon\\Db\\Adapter\\AdapterInterface;\nuse Phalcon\\Di\\DiInterface;\n\nfunction connectionServiceIsAdapter(DiInterface $di, string $service): bool\n{\n    return $di->getShared($service) instanceof AdapterInterface;\n}","tryCatchPattern":"use Phalcon\\Mvc\\Model\\Exceptions\\InvalidConnectionService;\n\ntry {\n    $connection = $manager->getConnection($model);\n} catch (InvalidConnectionService $e) {\n    // fail fast with actionable context; do not retry a misconfigured service\n    throw new RuntimeException(\n        'Service \"' . $model->getConnectionService() . '\" must return an AdapterInterface',\n        0,\n        $e\n    );\n}","preventionTips":["Make every connection factory return AdapterInterface on all code paths; never an array or null.","Add a boot-time smoke check: is_object($di->getShared($name)) for each configured connection name.","Cross-check model setConnectionService() names against registered DI services in a config test."],"tags":["phalcon","orm","dependency-injection","database-connection","service-registration"],"backgroundTag":"invalid-di-service","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}