@@ -57,7 +57,7 @@ private function assertNoIdMismatch($idFromRow, $id): void
57
57
}
58
58
}
59
59
60
- private function saveRow (Model $ model , array $ row , $ id, string $ table ): void
60
+ private function saveRow (Model $ model , array $ row , $ id ): void
61
61
{
62
62
if ($ model ->id_field ) {
63
63
$ idField = $ model ->getField ($ model ->id_field );
@@ -68,7 +68,7 @@ private function saveRow(Model $model, array $row, $id, string $table): void
68
68
}
69
69
}
70
70
71
- $ this ->data [$ table ][$ id ] = $ row ;
71
+ $ this ->data [$ model -> table ][$ id ] = $ row ;
72
72
}
73
73
74
74
private function addIdToLoadRow (Model $ model , array &$ row , $ id ): void
@@ -140,34 +140,37 @@ public function add(Model $model, array $defaults = []): Model
140
140
141
141
public function tryLoad (Model $ model , $ id , string $ table = null ): ?array
142
142
{
143
- $ table = $ table ?? $ model ->table ;
144
- if (!isset ($ this ->data [$ table ])) {
143
+ if ($ table !== null ) {
144
+ throw new \Error ('debug xxx!! ' . $ table . ' vs. ' . $ model ->table );
145
+ }
146
+
147
+ if (!isset ($ this ->data [$ model ->table ])) {
145
148
throw (new Exception ('Table was not found in the array data source ' ))
146
- ->addMoreInfo ('table ' , $ table );
149
+ ->addMoreInfo ('table ' , $ model -> table );
147
150
}
148
151
149
152
if ($ id === self ::ID_LOAD_ONE || $ id === self ::ID_LOAD_ANY ) {
150
- if (count ($ this ->data [$ table ]) === 0 ) {
153
+ if (count ($ this ->data [$ model -> table ]) === 0 ) {
151
154
return null ;
152
- } elseif ($ id === self ::ID_LOAD_ONE && count ($ this ->data [$ table ]) !== 1 ) {
155
+ } elseif ($ id === self ::ID_LOAD_ONE && count ($ this ->data [$ model -> table ]) !== 1 ) {
153
156
throw (new Exception ('Ambiguous conditions, more than one record can be loaded. ' ))
154
157
->addMoreInfo ('model ' , $ model )
155
158
->addMoreInfo ('id ' , null );
156
159
}
157
160
158
- $ id = array_key_first ($ this ->data [$ table ]);
161
+ $ id = array_key_first ($ this ->data [$ model -> table ]);
159
162
160
- $ row = $ this ->tryLoad ($ model , $ id, $ table );
163
+ $ row = $ this ->tryLoad ($ model , $ id );
161
164
$ model ->setId ($ id ); // @TODO is it needed?
162
165
163
166
return $ row ;
164
167
}
165
168
166
- if (!isset ($ this ->data [$ table ][$ id ])) {
169
+ if (!isset ($ this ->data [$ model -> table ][$ id ])) {
167
170
return null ;
168
171
}
169
172
170
- $ row = $ this ->data [$ table ][$ id ];
173
+ $ row = $ this ->data [$ model -> table ][$ id ];
171
174
$ this ->addIdToLoadRow ($ model , $ row , $ id );
172
175
173
176
return $ this ->typecastLoadRow ($ model , $ row );
@@ -180,13 +183,15 @@ public function tryLoad(Model $model, $id, string $table = null): ?array
180
183
*/
181
184
public function insert (Model $ model , array $ data , string $ table = null )
182
185
{
183
- $ table = $ table ?? $ model ->table ;
186
+ if ($ table !== null ) {
187
+ throw new \Error ('debug xxx!! ' . $ table . ' vs. ' . $ model ->table );
188
+ }
184
189
185
190
$ data = $ this ->typecastSaveRow ($ model , $ data );
186
191
187
- $ id = $ data [$ model ->id_field ] ?? $ this ->generateNewId ($ model, $ table );
192
+ $ id = $ data [$ model ->id_field ] ?? $ this ->generateNewId ($ model );
188
193
189
- $ this ->saveRow ($ model , $ data , $ id, $ table );
194
+ $ this ->saveRow ($ model , $ data , $ id );
190
195
191
196
return $ id ;
192
197
}
@@ -200,11 +205,13 @@ public function insert(Model $model, array $data, string $table = null)
200
205
*/
201
206
public function update (Model $ model , $ id , array $ data , string $ table = null )
202
207
{
203
- $ table = $ table ?? $ model ->table ;
208
+ if ($ table !== null ) {
209
+ throw new \Error ('debug xxx!! ' . $ table . ' vs. ' . $ model ->table );
210
+ }
204
211
205
212
$ data = $ this ->typecastSaveRow ($ model , $ data );
206
213
207
- $ this ->saveRow ($ model , array_merge ($ this ->data [$ table ][$ id ] ?? [], $ data ), $ id, $ table );
214
+ $ this ->saveRow ($ model , array_merge ($ this ->data [$ model -> table ][$ id ] ?? [], $ data ), $ id );
208
215
209
216
return $ id ;
210
217
}
@@ -216,9 +223,11 @@ public function update(Model $model, $id, array $data, string $table = null)
216
223
*/
217
224
public function delete (Model $ model , $ id , string $ table = null )
218
225
{
219
- $ table = $ table ?? $ model ->table ;
226
+ if ($ table !== null ) {
227
+ throw new \Error ('debug xxx!! ' . $ table . ' vs. ' . $ model ->table );
228
+ }
220
229
221
- unset($ this ->data [$ table ][$ id ]);
230
+ unset($ this ->data [$ model -> table ][$ id ]);
222
231
}
223
232
224
233
/**
@@ -228,13 +237,15 @@ public function delete(Model $model, $id, string $table = null)
228
237
*/
229
238
public function generateNewId (Model $ model , string $ table = null )
230
239
{
231
- $ table = $ table ?? $ model ->table ;
240
+ if ($ table !== null ) {
241
+ throw new \Error ('debug xxx!! ' . $ table . ' vs. ' . $ model ->table );
242
+ }
232
243
233
244
$ type = $ model ->id_field ? $ model ->getField ($ model ->id_field )->type : 'integer ' ;
234
245
235
246
switch ($ type ) {
236
247
case 'integer ' :
237
- $ ids = $ model ->id_field ? array_keys ($ this ->data [$ table ]) : [count ($ this ->data [$ table ])];
248
+ $ ids = $ model ->id_field ? array_keys ($ this ->data [$ model -> table ]) : [count ($ this ->data [$ model -> table ])];
238
249
239
250
$ id = $ ids ? max ($ ids ) + 1 : 1 ;
240
251
@@ -248,7 +259,7 @@ public function generateNewId(Model $model, string $table = null)
248
259
->addMoreInfo ('type ' , $ type );
249
260
}
250
261
251
- $ this ->lastInsertIds [$ table ] = $ id ;
262
+ $ this ->lastInsertIds [$ model -> table ] = $ id ;
252
263
$ this ->lastInsertIds ['$ ' ] = $ id ;
253
264
254
265
return $ id ;
@@ -310,7 +321,7 @@ public function initAction(Model $model, array $fields = null): \Atk4\Data\Actio
310
321
}
311
322
312
323
/**
313
- * Will set limit defined inside $m onto data.
324
+ * Will set limit defined inside $model onto data.
314
325
*/
315
326
protected function setLimitOrder (Model $ model , \Atk4 \Data \Action \Iterator $ action )
316
327
{
0 commit comments