@@ -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
@@ -138,36 +138,35 @@ public function add(Model $model, array $defaults = []): Model
138
138
return $ model ;
139
139
}
140
140
141
- public function tryLoad (Model $ model , $ id, string $ table = null ): ?array
141
+ public function tryLoad (Model $ model , $ id ): ?array
142
142
{
143
- $ table = $ table ?? $ model ->table ;
144
- if (!isset ($ this ->data [$ table ])) {
143
+ if (!isset ($ this ->data [$ model ->table ])) {
145
144
throw (new Exception ('Table was not found in the array data source ' ))
146
- ->addMoreInfo ('table ' , $ table );
145
+ ->addMoreInfo ('table ' , $ model -> table );
147
146
}
148
147
149
148
if ($ id === self ::ID_LOAD_ONE || $ id === self ::ID_LOAD_ANY ) {
150
- if (count ($ this ->data [$ table ]) === 0 ) {
149
+ if (count ($ this ->data [$ model -> table ]) === 0 ) {
151
150
return null ;
152
- } elseif ($ id === self ::ID_LOAD_ONE && count ($ this ->data [$ table ]) !== 1 ) {
151
+ } elseif ($ id === self ::ID_LOAD_ONE && count ($ this ->data [$ model -> table ]) !== 1 ) {
153
152
throw (new Exception ('Ambiguous conditions, more than one record can be loaded. ' ))
154
153
->addMoreInfo ('model ' , $ model )
155
154
->addMoreInfo ('id ' , null );
156
155
}
157
156
158
- $ id = array_key_first ($ this ->data [$ table ]);
157
+ $ id = array_key_first ($ this ->data [$ model -> table ]);
159
158
160
- $ row = $ this ->tryLoad ($ model , $ id, $ table );
159
+ $ row = $ this ->tryLoad ($ model , $ id );
161
160
$ model ->setId ($ id ); // @TODO is it needed?
162
161
163
162
return $ row ;
164
163
}
165
164
166
- if (!isset ($ this ->data [$ table ][$ id ])) {
165
+ if (!isset ($ this ->data [$ model -> table ][$ id ])) {
167
166
return null ;
168
167
}
169
168
170
- $ row = $ this ->data [$ table ][$ id ];
169
+ $ row = $ this ->data [$ model -> table ][$ id ];
171
170
$ this ->addIdToLoadRow ($ model , $ row , $ id );
172
171
173
172
return $ this ->typecastLoadRow ($ model , $ row );
@@ -178,15 +177,13 @@ public function tryLoad(Model $model, $id, string $table = null): ?array
178
177
*
179
178
* @return mixed
180
179
*/
181
- public function insert (Model $ model , array $ data, string $ table = null )
180
+ public function insert (Model $ model , array $ data )
182
181
{
183
- $ table = $ table ?? $ model ->table ;
184
-
185
182
$ data = $ this ->typecastSaveRow ($ model , $ data );
186
183
187
- $ id = $ data [$ model ->id_field ] ?? $ this ->generateNewId ($ model, $ table );
184
+ $ id = $ data [$ model ->id_field ] ?? $ this ->generateNewId ($ model );
188
185
189
- $ this ->saveRow ($ model , $ data , $ id, $ table );
186
+ $ this ->saveRow ($ model , $ data , $ id );
190
187
191
188
return $ id ;
192
189
}
@@ -198,13 +195,11 @@ public function insert(Model $model, array $data, string $table = null)
198
195
*
199
196
* @return mixed
200
197
*/
201
- public function update (Model $ model , $ id , array $ data, string $ table = null )
198
+ public function update (Model $ model , $ id , array $ data )
202
199
{
203
- $ table = $ table ?? $ model ->table ;
204
-
205
200
$ data = $ this ->typecastSaveRow ($ model , $ data );
206
201
207
- $ this ->saveRow ($ model , array_merge ($ this ->data [$ table ][$ id ] ?? [], $ data ), $ id, $ table );
202
+ $ this ->saveRow ($ model , array_merge ($ this ->data [$ model -> table ][$ id ] ?? [], $ data ), $ id );
208
203
209
204
return $ id ;
210
205
}
@@ -214,27 +209,23 @@ public function update(Model $model, $id, array $data, string $table = null)
214
209
*
215
210
* @param mixed $id
216
211
*/
217
- public function delete (Model $ model , $ id, string $ table = null )
212
+ public function delete (Model $ model , $ id )
218
213
{
219
- $ table = $ table ?? $ model ->table ;
220
-
221
- unset($ this ->data [$ table ][$ id ]);
214
+ unset($ this ->data [$ model ->table ][$ id ]);
222
215
}
223
216
224
217
/**
225
218
* Generates new record ID.
226
219
*
227
220
* @return string
228
221
*/
229
- public function generateNewId (Model $ model, string $ table = null )
222
+ public function generateNewId (Model $ model )
230
223
{
231
- $ table = $ table ?? $ model ->table ;
232
-
233
224
$ type = $ model ->id_field ? $ model ->getField ($ model ->id_field )->type : 'integer ' ;
234
225
235
226
switch ($ type ) {
236
227
case 'integer ' :
237
- $ ids = $ model ->id_field ? array_keys ($ this ->data [$ table ]) : [count ($ this ->data [$ table ])];
228
+ $ ids = $ model ->id_field ? array_keys ($ this ->data [$ model -> table ]) : [count ($ this ->data [$ model -> table ])];
238
229
239
230
$ id = $ ids ? max ($ ids ) + 1 : 1 ;
240
231
@@ -248,7 +239,7 @@ public function generateNewId(Model $model, string $table = null)
248
239
->addMoreInfo ('type ' , $ type );
249
240
}
250
241
251
- $ this ->lastInsertIds [$ table ] = $ id ;
242
+ $ this ->lastInsertIds [$ model -> table ] = $ id ;
252
243
$ this ->lastInsertIds ['$ ' ] = $ id ;
253
244
254
245
return $ id ;
@@ -310,7 +301,7 @@ public function initAction(Model $model, array $fields = null): \Atk4\Data\Actio
310
301
}
311
302
312
303
/**
313
- * Will set limit defined inside $m onto data.
304
+ * Will set limit defined inside $model onto data.
314
305
*/
315
306
protected function setLimitOrder (Model $ model , \Atk4 \Data \Action \Iterator $ action )
316
307
{
0 commit comments