|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Reconmap\Repositories; |
| 4 | + |
| 5 | +use Ponup\SqlBuilders\SelectQueryBuilder; |
| 6 | +use Reconmap\Models\CustomField; |
| 7 | + |
| 8 | +class CustomFieldRepository extends MysqlRepository implements Deletable |
| 9 | +{ |
| 10 | + public const array UPDATABLE_COLUMNS_TYPES = [ |
| 11 | + 'parent_type' => 's', |
| 12 | + 'name' => 's', |
| 13 | + 'label' => 's', |
| 14 | + 'kind' => 's', |
| 15 | + 'config' => 's', |
| 16 | + ]; |
| 17 | + |
| 18 | + public function findByParentId(string $parentType, ?int $parentId): array |
| 19 | + { |
| 20 | + $queryBuilder = $this->getBaseSelectQueryBuilder(); |
| 21 | + if (is_null($parentId)) { |
| 22 | + $queryBuilder->setWhere('n.parent_type = ? AND n.parent_id IS NULL'); |
| 23 | + } else { |
| 24 | + $queryBuilder->setWhere('n.parent_type = ? AND n.parent_id = ?'); |
| 25 | + } |
| 26 | + |
| 27 | + $stmt = $this->db->prepare($queryBuilder->toSql()); |
| 28 | + if (is_null($parentId)) { |
| 29 | + $stmt->bind_param('s', $parentType); |
| 30 | + } else { |
| 31 | + $stmt->bind_param('si', $parentType, $parentId); |
| 32 | + } |
| 33 | + $stmt->execute(); |
| 34 | + $result = $stmt->get_result(); |
| 35 | + $documents = $result->fetch_all(MYSQLI_ASSOC); |
| 36 | + $stmt->close(); |
| 37 | + |
| 38 | + return $documents; |
| 39 | + } |
| 40 | + |
| 41 | + public function findById(int $id): ?array |
| 42 | + { |
| 43 | + $queryBuilder = $this->getBaseSelectQueryBuilder(); |
| 44 | + $queryBuilder->setWhere('n.id = ?'); |
| 45 | + $stmt = $this->db->prepare($queryBuilder->toSql()); |
| 46 | + $stmt->bind_param('i', $id); |
| 47 | + $stmt->execute(); |
| 48 | + $result = $stmt->get_result(); |
| 49 | + $document = $result->fetch_assoc(); |
| 50 | + $stmt->close(); |
| 51 | + |
| 52 | + return $document; |
| 53 | + } |
| 54 | + |
| 55 | + public function deleteById(int $id): bool |
| 56 | + { |
| 57 | + return $this->deleteByTableId('custom_field', $id); |
| 58 | + } |
| 59 | + |
| 60 | + public function insert(CustomField $customField): bool |
| 61 | + { |
| 62 | + $stmt = $this->db->prepare('INSERT INTO custom_field (parent_type, name, label, kind, config) VALUES (?, ?, ?, ?, ?)'); |
| 63 | + return $stmt->execute([$customField->parent_type, $customField->name, $customField->label, $customField->kind, $customField->config]); |
| 64 | + } |
| 65 | + |
| 66 | + protected function getBaseSelectQueryBuilder(): SelectQueryBuilder |
| 67 | + { |
| 68 | + $queryBuilder = new SelectQueryBuilder('custom_field AS cf'); |
| 69 | + $queryBuilder->setOrderBy('cf.insert_ts DESC'); |
| 70 | + |
| 71 | + return $queryBuilder; |
| 72 | + } |
| 73 | + |
| 74 | + public function findAll(): array |
| 75 | + { |
| 76 | + $sql = $this->getBaseSelectQueryBuilder()->toSql(); |
| 77 | + $resultSet = $this->db->query($sql); |
| 78 | + return $resultSet->fetch_all(MYSQLI_ASSOC); |
| 79 | + } |
| 80 | + |
| 81 | + public function updateById(int $id, array $newColumnValues): bool |
| 82 | + { |
| 83 | + return $this->updateByTableId('custom_field', $id, $newColumnValues); |
| 84 | + } |
| 85 | +} |
0 commit comments