{"record":{"id":"be07d4d405d1f39d","repo":"phalcon/cphalcon","slug":"only-phql-statements-that-return-resultsets-can-be","errorCode":null,"errorMessage":"Only PHQL statements that return resultsets can be cached","messagePattern":"Only PHQL statements that return resultsets can be cached","errorType":"exception","errorClass":"Phalcon\\Mvc\\Model\\Query\\Exceptions\\ResultsetNonCacheable","httpStatus":null,"severity":"error","filePath":"phalcon/Mvc/Model/Query.zep","lineNumber":475,"sourceCode":"                    mergedParams,\n                    mergedTypes\n                );\n\n                break;\n\n            default:\n                throw new UnknownPhqlStatement(type);\n        }\n\n        /**\n         * We store the resultset in the cache if any\n         */\n        if cacheOptions !== null {\n            /**\n             * Only PHQL SELECTs can be cached\n             */\n            if type != PHQL_T_SELECT {\n                throw new ResultsetNonCacheable();\n            }\n\n            cache->set(key, result, lifetime);\n        }\n\n        /**\n         * Check if only the first row must be returned\n         */\n        if uniqueRow {\n            let preparedResult = result->getFirst();\n        } else {\n            let preparedResult = result;\n        }\n\n        return preparedResult;\n    }\n\n    /**","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Mvc/Model/Query.zep#L457-L493","documentation":"After executing, Query::execute() only caches results when cacheOptions are set, and it requires statement type PHQL_T_SELECT; caching an INSERT/UPDATE/DELETE result raises ResultsetNonCacheable. The faulty input is a cache() configuration applied to a non-SELECT PHQL statement.","triggerScenarios":"Thrown at phalcon/Mvc/Model/Query.zep:475 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Only attach cache() to SELECT queries","Remove the cache() call from insert/update/delete queries","Conditionally apply cache options only for read paths"],"exampleFix":"if ($isReadQuery) { $builder->cache(['key' => $cacheKey]); }","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Encapsulate caching in repository read methods only","Invalidate related cache keys on writes instead of caching writes"],"tags":[],"backgroundTag":null,"analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","contentChangedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}