{"record":{"id":"4235144cc0092edd","repo":"passbolt/passbolt_api","slug":"could-not-validate-entity-history-data","errorCode":null,"errorMessage":"Could not validate entity history data.","messagePattern":"Could not validate entity history data\\.","errorType":"validation","errorClass":"ValidationException","httpStatus":400,"severity":"error","filePath":"plugins/PassboltCe/Log/src/Model/Table/EntitiesHistoryTable.php","lineNumber":200,"sourceCode":"     * Create a new entity_history.\n     *\n     * @param array $data the data: foreign_key and foreign_model\n     * @param \\App\\Utility\\UserAction $userAction userAction object\n     * @return \\Passbolt\\Log\\Model\\Entity\\EntityHistory\n     * @throws \\App\\Error\\Exception\\ValidationException\n     * @throws \\Cake\\Http\\Exception\\InternalErrorException\n     */\n    public function create(array $data, UserAction $userAction): EntityHistory\n    {\n        $defaultData = [\n            'action_log_id' => $userAction->getUserActionId(),\n        ];\n        $data = array_merge($defaultData, $data);\n\n        // Check validation rules.\n        $log = $this->buildEntity($data);\n        if ($log->getErrors()) {\n            throw new ValidationException(__('Could not validate entity history data.', true), $log, $this);\n        }\n\n        $entityHistory = $this->save($log, ['associated' => ['PermissionsHistory']]);\n\n        // Check for validation errors. (associated models too).\n        if ($log->getErrors()) {\n            throw new ValidationException(__('Could not validate entity history data.'), $entityHistory, $this);\n        }\n\n        // Check for errors while saving.\n        if (!$entityHistory) {\n            throw new InternalErrorException('Could not save the entity history.');\n        }\n\n        return $entityHistory;\n    }\n}\n","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/plugins/PassboltCe/Log/src/Model/Table/EntitiesHistoryTable.php#L182-L218","documentation":"EntitiesHistoryTable::create() throws this ValidationException when the entity history entity built via buildEntity() fails validation rules before saving. The supplied data (e.g. entity type, foreign model, foreign key, or crud flag) violates the table's ruleset.","triggerScenarios":"create() called with missing/invalid fields: unknown entity type (foreign_model), empty foreign_key, invalid crud value, or data merged over $defaultData that still lacks required columns.","commonSituations":"Plugins logging entity changes with a foreign_model not in the allowed list; passing an integer foreign key where a UUID string is expected; forgetting the 'crud' field when merging custom data over defaults; after a schema change that made a formerly-optional field required.","solutions":["Inspect the entity errors attached to the ValidationException to identify the failing field.","Ensure required fields are present: foreign_model, foreign_key, crud, and any per-type fields.","Verify the entity type/foreign model string matches the expected constants exactly.","If the data is legitimate, adjust the validation ruleset rather than bypassing validation."],"exampleFix":"// before\n$this->EntitiesHistory->create([\n    'foreign_model' => 'Permission',\n    'foreign_key' => $permissionId,\n]);\n// after\n$this->EntitiesHistory->create([\n    'foreign_model' => 'Permission',\n    'foreign_key' => $permissionId,\n    'crud' => 'u',\n]);","handlingStrategy":"validation","validationCode":"function validateEntityHistoryData(array $data): bool { return !empty($data['foreign_model']) && !empty($data['foreign_key']) && in_array($data['crud'] ?? null, ['c','u','d'], true); }","typeGuard":"function isEntityHistoryPayload(mixed $data): bool { return is_array($data) && isset($data['foreign_model'], $data['foreign_key'], $data['crud']); }","tryCatchPattern":"try { $eh = $entitiesHistoryTable->create($data); } catch (ValidationException $e) { $errors = $e->getEntity()->getErrors(); // fix payload before retrying\n}","preventionTips":["Always merge required fields on top of the table's $defaultData, not instead of them.","Use the plugin's entity type constants for foreign_model values.","Pass UUID strings (not ints) for foreign_key.","Add unit tests for each entity type your plugin logs."],"tags":["validation","logging","entity-history","cakephp"],"backgroundTag":"schema-validation-failed","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}