wpdb->prepare( "SELECT COUNT(*) FROM {$this->table} WHERE id = %d", $entryFieldId ); return (bool) $this->wpdb->get_var($query); } /** * @throws \Exception */ public function add(object $entity): int { $data = $entity->toArray(); $result = $this->wpdb->insert( $this->table, [ 'id' => $data['id'] ?? null, 'entryId' => $data['entryId'], 'fieldId' => $data['fieldId'], 'fieldValue' => $data['fieldValue'], ], [ '%d', '%d', '%d', '%s', ], ); if ($result === false) { throw new Exception( BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ ); } return $this->wpdb->insert_id; } /** * @throws Exception */ public function update(int $id, object $entity): bool { $data = $entity->toArray(); $result = $this->wpdb->update( $this->table, [ 'fieldId' => $data['fieldId'], 'fieldValue' => $data['fieldValue'], ], ['id' => $id], [ '%d', '%s', ], ['%d'] ); if ($result === false) { throw new Exception( BackendStrings::getExceptionStrings()['unable_update_data'] . __CLASS__ ); } return (bool) $result; } /** * Find an entry field by its ID. * * @param array $array * @return array */ public function findBy(array $array): array { $sql = $this->selectQuery() . " {$this->table}"; $where = []; $values = []; foreach ($array as $field => $value) { $where[] = "{$field} = %s"; $values[] = $value; } if ($where) { $sql .= ' WHERE ' . implode(' AND ', $where); } $query = $this->wpdb->prepare($sql, ...$values); $rows = $this->wpdb->get_results($query, ARRAY_A); $results = []; foreach ($rows as $row) { $results[] = call_user_func([static::FACTORY, 'create'], $row); } return $results; } /** * Get all entry field IDs for the given entry IDs in a single query. * * @param int[] $entryIds * @return int[] */ public function findIdsByEntryIds(array $entryIds): array { if (empty($entryIds)) { return []; } $placeholders = implode(',', array_fill(0, count($entryIds), '%d')); $query = "SELECT id FROM {$this->table} WHERE entryId IN ($placeholders)"; $results = $this->wpdb->get_results($this->wpdb->prepare($query, ...$entryIds), ARRAY_A); return array_map(fn($row) => (int)$row['id'], $results); } }