phalcon/cphalcon · error · WriteConnectionMissing

selectWriteConnection did not return a connection

Error message

selectWriteConnection did not return a connection

What it means

Thrown in Query::getWriteConnection when the model implements selectWriteConnection() but the method returned a non-object, leaving the query without a valid write connection adapter for INSERT/UPDATE/DELETE execution.

Solutions

  1. Ensure selectWriteConnection() always returns an AdapterInterface, including error branches.
  2. Log inputs (intermediate, bindParams, bindTypes) inside selectWriteConnection to find the null path.
  3. Fall back to model->getWriteConnection() when custom selection cannot resolve.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at phalcon/Mvc/Model/Query.zep:3694 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/6ac29ee25c0f9b69. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model/Query.zep:3694

    protected function getWriteConnection(<ModelInterface> model, array intermediate = null, array bindParams = [], array bindTypes = []) -> <AdapterInterface>
    {
        var connection = null, transaction;

        let transaction = this->transaction;

        if typeof transaction == "object" && transaction instanceof TransactionInterface {
            return transaction->getConnection();
        }

        if method_exists(model, "selectWriteConnection") {
            let connection = model->selectWriteConnection(
                intermediate,
                bindParams,
                bindTypes
            );

            if unlikely typeof connection != "object" {
                throw new WriteConnectionMissing();
            }

            return connection;
        }
        return model->getWriteConnection();
    }

    /**
     * Analyzes a DELETE intermediate code and produces an array to be executed
     * later
     */
    final protected function prepareDelete() -> array
    {
        var ast, delete, tables, models, modelsInstances, sqlTables, sqlModels,
            sqlAliases, sqlAliasesModelsInstances, deleteTables, manager, table,
            qualifiedName, modelName, model, source, schema, completeSource,
            alias, sqlDelete, where, limit;

View on GitHub (pinned to b7419de9cd)