1 | <?php |
||
2 | |||
3 | namespace HexMakina\TightORM; |
||
4 | |||
5 | use HexMakina\Crudites\Crudites; |
||
6 | use HexMakina\Crudites\Interfaces\TableManipulationInterface; |
||
7 | |||
8 | abstract class TableModel extends Crudites |
||
9 | { |
||
10 | |||
11 | //check all primary keys are set (FIXME that doesn't work unles AIPK.. nice try) |
||
12 | public function is_new(): bool |
||
13 | { |
||
14 | $match = static::table()->primary_keys_match(get_object_vars($this)); |
||
15 | return empty($match); |
||
16 | } |
||
17 | |||
18 | public function get_id($mode = null) |
||
19 | { |
||
20 | $primary_key = static::table()->auto_incremented_primary_key(); |
||
21 | if (is_null($primary_key) && count($pks = static::table()->primary_keys()) == 1) { |
||
22 | $primary_key = current($pks); |
||
23 | } |
||
24 | |||
25 | return $mode === 'name' ? $primary_key->name() : $this->get($primary_key->name()); |
||
26 | } |
||
27 | |||
28 | public function get($prop_name) |
||
29 | { |
||
30 | if (property_exists($this, $prop_name) === true) { |
||
31 | return $this->$prop_name; |
||
32 | } |
||
33 | |||
34 | return null; |
||
35 | } |
||
36 | |||
37 | public function set($prop_name, $value) |
||
38 | { |
||
39 | $this->$prop_name = $value; |
||
40 | } |
||
41 | |||
42 | public function import($assoc_data) |
||
43 | { |
||
44 | if (!is_array($assoc_data)) { |
||
45 | throw new \Exception(__FUNCTION__ . '(assoc_data) parm is not an array'); |
||
46 | } |
||
47 | |||
48 | // shove it all up in model, god will sort them out |
||
49 | foreach ($assoc_data as $field => $value) { |
||
50 | $this->set($field, $value); |
||
51 | } |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | public static function table(): TableManipulationInterface |
||
57 | { |
||
58 | $table = static::table_name(); |
||
59 | $table = self::inspect($table); |
||
60 | |||
61 | return $table; |
||
62 | } |
||
63 | |||
64 | public static function table_name(): string |
||
65 | { |
||
66 | $reflect = new \ReflectionClass(get_called_class()); |
||
67 | |||
68 | $table_name = $reflect->getConstant('TABLE_NAME'); |
||
69 | |||
70 | if ($table_name === false) { |
||
71 | $calling_class = $reflect->getShortName(); |
||
72 | if (defined($const_name = 'TABLE_' . strtoupper($calling_class))) { |
||
73 | $table_name = constant($const_name); |
||
74 | } else { |
||
75 | $table_name = strtolower($calling_class); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return $table_name; |
||
80 | } |
||
81 | |||
82 | |||
83 | public function to_table_row($operator_id = null) |
||
84 | { |
||
85 | if (!is_null($operator_id) && $this->is_new() && is_null($this->get('created_by'))) { |
||
86 | $this->set('created_by', $operator_id); |
||
87 | } |
||
88 | |||
89 | $model_data = get_object_vars($this); |
||
90 | |||
91 | // 1. Produce OR restore a row |
||
92 | if ($this->is_new()) { |
||
93 | $table_row = static::table()->produce($model_data); |
||
94 | } else { |
||
95 | $table_row = static::table()->restore($model_data); |
||
96 | } |
||
97 | |||
98 | // 2. Apply alterations from form_model data |
||
99 | $table_row->alter($model_data); |
||
100 | |||
101 | return $table_row; |
||
102 | } |
||
103 | |||
104 | |||
105 | public static function query_retrieve($filters = [], $options = []): SelectInterface |
||
0 ignored issues
–
show
|
|||
106 | { |
||
107 | return static::table()->select(); |
||
0 ignored issues
–
show
|
|||
108 | } |
||
109 | |||
110 | |||
111 | // success: return PK-indexed array of results (associative array or object) |
||
112 | public static function retrieve(SelectInterface $Query): array |
||
113 | { |
||
114 | $ret = []; |
||
115 | $pk_name = implode('_', array_keys($Query->table()->primary_keys())); |
||
116 | |||
117 | if (count($pks = $Query->table()->primary_keys()) > 1) { |
||
118 | $concat_pk = sprintf('CONCAT(%s) as %s', implode(',', $pks), $pk_name); |
||
119 | $Query->select_also([$concat_pk]); |
||
120 | } |
||
121 | |||
122 | try { |
||
123 | $Query->run(); |
||
124 | } catch (CruditesException $e) { |
||
0 ignored issues
–
show
The type
HexMakina\TightORM\CruditesException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
125 | return []; |
||
126 | } |
||
127 | |||
128 | if ($Query->is_success()) { |
||
129 | foreach ($Query->ret_obj(get_called_class()) as $rec) { |
||
130 | $ret[$rec->get($pk_name)] = $rec; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return $ret; |
||
135 | } |
||
136 | |||
137 | /* USAGE |
||
138 | * one($primary_key_value) |
||
139 | * one($unique_column, $value) |
||
140 | */ |
||
141 | public static function one($arg1, $arg2 = null) |
||
142 | { |
||
143 | $mixed_info = is_null($arg2) ? $arg1 : [$arg1 => $arg2]; |
||
144 | |||
145 | $unique_identifiers = get_called_class()::table()->match_uniqueness($mixed_info); |
||
146 | |||
147 | if (empty($unique_identifiers)) { |
||
148 | throw new CruditesException('UNIQUE_IDENTIFIER_NOT_FOUND'); |
||
149 | } |
||
150 | |||
151 | $Query = static::query_retrieve([], ['eager' => true])->aw_fields_eq($unique_identifiers); |
||
152 | switch (count($res = static::retrieve($Query))) { |
||
153 | case 0: |
||
154 | throw new CruditesException('INSTANCE_NOT_FOUND'); |
||
155 | case 1: |
||
156 | return current($res); |
||
157 | default: |
||
158 | throw new CruditesException('SINGULAR_INSTANCE_ERROR'); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | public static function exists($arg1, $arg2 = null) |
||
163 | { |
||
164 | try { |
||
165 | return self::one($arg1, $arg2); |
||
166 | } catch (CruditesException $e) { |
||
167 | return null; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | |||
172 | public static function any($field_exact_values, $options = []) |
||
173 | { |
||
174 | $Query = static::query_retrieve([], $options)->aw_fields_eq($field_exact_values); |
||
175 | return static::retrieve($Query); |
||
176 | } |
||
177 | |||
178 | public static function filter($filters = [], $options = []): array |
||
179 | { |
||
180 | return static::retrieve(static::query_retrieve($filters, $options)); |
||
181 | } |
||
182 | |||
183 | public static function listing($filters = [], $options = []): array |
||
184 | { |
||
185 | return static::retrieve(static::query_retrieve($filters, $options)); // listing as arrays for templates |
||
186 | } |
||
187 | |||
188 | |||
189 | |||
190 | public static function get_many_by_AIPK($aipk_values) |
||
191 | { |
||
192 | if (!empty($aipk_values) && !is_null($AIPK = static::table()->auto_incremented_primary_key())) { |
||
193 | return static::retrieve(static::table()->select()->aw_numeric_in($AIPK, $aipk_values)); |
||
194 | } |
||
195 | |||
196 | return null; |
||
197 | } |
||
198 | |||
199 | |||
200 | } |
||
201 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths