{"record":{"id":"ff4d4c9e13784ea3","repo":"phalcon/cphalcon","slug":"entity-must-be-an-object","errorCode":null,"errorMessage":"Entity must be an object","messagePattern":"Entity must be an object","errorType":"exception","errorClass":"Phalcon\\Filter\\Validation\\Exceptions\\ValidationEntityNotObject","httpStatus":null,"severity":"error","filePath":"phalcon/Filter/Validation.zep","lineNumber":548,"sourceCode":"    {\n        var localMessages;\n\n        let localMessages = self::defaultMessages;\n\n        let self::defaultMessages = array_merge(localMessages, messages);\n\n        return self::defaultMessages;\n    }\n\n    /**\n     * Sets the bound entity\n     *\n     * @param object entity\n     */\n    public function setEntity(entity) -> void\n    {\n        if unlikely typeof entity != \"object\" {\n            throw new ValidationEntityNotObject();\n        }\n\n        let this->entity = entity;\n    }\n\n    /**\n     * Adds filters to the field\n     *\n     * @param string field\n     * @param array|string filters\n     */\n    public function setFilters(var field, filters) -> <static>\n    {\n        var singleField;\n\n        if typeof field == \"array\" {\n            for singleField in field {\n                let this->filters[singleField] = filters;","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Filter/Validation.zep#L530-L566","documentation":"Validation::setEntity(entity) binds an object (typically a model) whose getters/setters supply field values during validation. ValidationEntityNotObject is thrown when anything other than an object is passed — null, array, string, or scalar.","triggerScenarios":"$validation->setEntity(User::findFirst($id)) where findFirst() returns null/false for a missing record; $validation->setEntity($request->getPost()) passing an array; setEntity($data['user'] ?? null) defaulting to null.","commonSituations":"Null-coalesced model fetches feeding setEntity; treating arrays as entities instead of using bind(); 'not found' branches that continue to the validation step.","solutions":["Guard the null case before binding: if (!$user) { handle not-found; } else { $validation->setEntity($user); }","For array data use bind(null, $arrayData) or validate($arrayData) instead of setEntity()","Type-hint the caller: setEntity(object $entity) at your own wrapper"],"exampleFix":"// before\n$user = User::findFirst($id); // null when missing\n$validation->setEntity($user); // throws ValidationEntityNotObject\n\n// after\n$user = User::findFirst($id);\nif ($user === null) {\n    throw new NotFoundException(\"User {$id} not found\");\n}\n$validation->setEntity($user);","handlingStrategy":"type-guard","validationCode":"if (!is_object($entity)) {\n    throw new InvalidArgumentException('Entity must be an object, got ' . get_debug_type($entity));\n}\n$validation->setEntity($entity);","typeGuard":"function isEntityCandidate($entity): bool\n{\n    return is_object($entity);\n}","tryCatchPattern":"use Phalcon\\Filter\\Validation\\Exceptions\\ValidationEntityNotObject;\ntry {\n    $v->setEntity($entity);\n} catch (ValidationEntityNotObject $e) {\n    // missing record or wrong payload shape: treat as not-found, not as 500\n    throw new NotFoundException('Entity to validate does not exist', 0, $e);\n}","preventionTips":["Check finder results for null before binding: if (!$model) return 404","Use bind(null, $arrayData) for array payloads — setEntity() is for objects only"],"tags":["phalcon","validation","entity","type-error","null-safety"],"backgroundTag":"invalid-entity-type","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}