groupStart() ->like('comment_month', $keyword) ->orLike('com_text', $keyword) ->groupEnd() ->findAll(); } return $this->findAll(); } /** * Get comments by control, test and month */ public function getByControlTestMonth(int $controlId, int $testId, string $month): ?array { return $this->where('control_id', $controlId) ->where('test_id', $testId) ->where('comment_month', $month) ->where('deleted_at', null) ->first(); } /** * Get all comments for a test and month */ public function getByTestMonth(int $testId, string $month): array { return $this->where('test_id', $testId) ->where('comment_month', $month) ->where('deleted_at', null) ->findAll(); } /** * Upsert comment (insert or update based on control/test/month) */ public function upsertComment(array $data): int { $existing = $this->where('control_id', $data['control_id']) ->where('test_id', $data['test_id']) ->where('comment_month', $data['comment_month']) ->where('deleted_at', null) ->first(); if ($existing) { if (empty($data['com_text'])) { // If text is empty, soft delete $this->update($existing['result_comment_id'], ['deleted_at' => date('Y-m-d H:i:s')]); return $existing['result_comment_id']; } $this->update($existing['result_comment_id'], $data); return $existing['result_comment_id']; } else { if (empty($data['com_text'])) { return 0; // Don't insert empty comments } return $this->insert($data, true); } } }