2
2
namespace BNETDocs \Libraries ;
3
3
4
4
use \BNETDocs \Libraries \Exceptions \PacketNotFoundException ;
5
+ use \BNETDocs \Libraries \Exceptions \ProductNotFoundException ;
5
6
use \BNETDocs \Libraries \IDatabaseObject ;
6
7
use \BNETDocs \Libraries \Packet \Application as ApplicationLayer ;
7
8
use \BNETDocs \Libraries \Packet \Transport as TransportLayer ;
9
+ use \BNETDocs \Libraries \Product ;
8
10
use \BNETDocs \Libraries \User ;
9
11
use \CarlBennett \MVC \Libraries \Common ;
10
12
use \CarlBennett \MVC \Libraries \DatabaseDriver ;
@@ -116,6 +118,7 @@ public function allocate()
116
118
$ this ->setPacketId (0 );
117
119
$ this ->setRemarks ('' );
118
120
$ this ->setTransportLayerId (self ::DEFAULT_TRANSPORT_LAYER_ID );
121
+ $ this ->setUsedBy ([]);
119
122
120
123
if (is_null ($ id )) return ;
121
124
@@ -154,10 +157,26 @@ public function allocate()
154
157
throw new UnexpectedValueException (sprintf ('packet id: %d not found ' , $ id ));
155
158
}
156
159
157
- $ r = $ q ->fetchObject ();
160
+ $ o = $ q ->fetchObject ();
158
161
$ q ->closeCursor ();
159
162
160
- $ this ->allocateObject ($ r );
163
+ $ q = Common::$ database ->prepare ('
164
+ SELECT `u`.`bnet_product_id` AS `used_by` FROM `packet_used_by` AS `u`
165
+ INNER JOIN `products` AS `p` ON `u`.`bnet_product_id` = `p`.`bnet_product_id`
166
+ WHERE `u`.`id` = ? ORDER BY `p`.`sort` ASC;
167
+ ' );
168
+ $ r = $ q ->execute ([(int ) $ this ->id ]);
169
+ if (!$ r ) return $ r ;
170
+
171
+ $ used_by = [];
172
+ while ($ row = $ q ->fetch (PDO ::FETCH_NUM ))
173
+ {
174
+ $ used_by [] = new Product ((int ) $ row [0 ]);
175
+ }
176
+ $ o ->used_by = $ used_by ;
177
+ $ q ->closeCursor ();
178
+
179
+ $ this ->allocateObject ($ o );
161
180
}
162
181
163
182
/**
@@ -182,6 +201,8 @@ protected function allocateObject(StdClass $value)
182
201
$ this ->setRemarks ($ value ->packet_remarks );
183
202
$ this ->setTransportLayerId ($ value ->packet_transport_layer_id );
184
203
$ this ->setUserId ($ value ->user_id );
204
+ if (!isset ($ value ->used_by )) throw new \RuntimeException ();
205
+ $ this ->setUsedBy ($ value ->used_by );
185
206
}
186
207
187
208
/**
@@ -225,9 +246,7 @@ public function commit()
225
246
`packet_remarks` = :r,
226
247
`packet_transport_layer_id` = :tr_id,
227
248
`user_id` = :uid
228
- ;
229
- SELECT LAST_INSERT_ID();
230
- ' );
249
+ ; ' );
231
250
232
251
$ created_datetime = $ this ->created_datetime ->format (self ::DATE_SQL );
233
252
@@ -252,10 +271,29 @@ public function commit()
252
271
$ r = $ q ->execute ();
253
272
if (!$ r ) return $ r ;
254
273
255
- $ q ->nextRowset ();
256
- $ this ->setId ($ q ->fetch (PDO ::FETCH_NUM )[0 ]);
274
+ if (is_null ($ this ->id ))
275
+ {
276
+ $ this ->setId (Common::$ database ->lastInsertId ());
277
+ }
278
+
279
+ $ q = Common::$ database ->prepare ('DELETE FROM `packet_used_by` WHERE `id` = :id; ' );
280
+ $ q ->bindParam (':id ' , $ this ->id , PDO ::PARAM_INT );
281
+ $ r = $ q ->execute ();
282
+ if (!$ r ) return $ r ;
283
+
284
+ $ q = Common::$ database ->prepare ('INSERT INTO `packet_used_by` (`id`, `bnet_product_id`) VALUES (:id, :p); ' );
285
+ foreach ($ this ->used_by as $ v )
286
+ {
287
+ $ p = $ v ->getBnetProductId ();
288
+ $ q ->bindParam (':id ' , $ this ->id , PDO ::PARAM_INT );
289
+ $ q ->bindParam (':p ' , $ p , PDO ::PARAM_INT );
290
+ $ r = $ q ->execute ();
291
+ if (!$ r )
292
+ {
293
+ return $ r ;
294
+ }
295
+ }
257
296
258
- $ q ->closeCursor ();
259
297
return $ r ;
260
298
}
261
299
@@ -342,32 +380,17 @@ public static function getPacketsByLastEdited(int $count)
342
380
Common::$ database = DatabaseDriver::getDatabaseObject ();
343
381
}
344
382
345
- $ q = Common::$ database ->prepare (sprintf ('
346
- SELECT
347
- `created_datetime`,
348
- `edited_count`,
349
- `edited_datetime`,
350
- `id`,
351
- `options_bitmask`,
352
- `packet_application_layer_id`,
353
- `packet_direction_id`,
354
- `packet_format`,
355
- `packet_id`,
356
- `packet_name`,
357
- `packet_remarks`,
358
- `packet_transport_layer_id`,
359
- `user_id`
360
- FROM `packets`
361
- ORDER BY IFNULL(`edited_datetime`, `created_datetime`) DESC LIMIT %s; ' , $ count
383
+ $ q = Common::$ database ->prepare (sprintf (
384
+ 'SELECT `id` FROM `packets` ORDER BY IFNULL(`edited_datetime`, `created_datetime`) DESC LIMIT %d; ' , $ count
362
385
));
363
386
364
387
$ r = $ q ->execute ();
365
388
if (!$ r ) return $ r ;
366
389
367
390
$ r = [];
368
- while ($ row = $ q ->fetch (PDO ::FETCH_OBJ ))
391
+ while ($ row = $ q ->fetch (PDO ::FETCH_NUM ))
369
392
{
370
- $ r [] = new self ($ row );
393
+ $ r [] = new self ($ row[ 0 ] );
371
394
}
372
395
373
396
$ q ->closeCursor ();
@@ -964,42 +987,18 @@ public function setTransportLayerId(int $value)
964
987
/**
965
988
* Sets the products this Packet is used by.
966
989
*
967
- * @param array $value The set of ProductId integers.
990
+ * @param array $value The set of Product objects, or Product::$id integers.
968
991
*/
969
992
public function setUsedBy (array $ value )
970
993
{
971
- if (!isset (Common::$ database ))
972
- {
973
- Common::$ database = DatabaseDriver::getDatabaseObject ();
974
- }
975
-
976
- try
977
- {
978
- Common::$ database ->beginTransaction ();
979
-
980
- $ q = Common::$ database ->prepare ('DELETE FROM `packet_used_by` WHERE `id` = :id; ' );
981
- $ q ->bindParam (':id ' , $ this ->id , PDO ::PARAM_INT );
982
- $ r = $ q ->execute ();
983
- if (!$ r ) return $ r ;
994
+ $ used_by = [];
984
995
985
- $ q = Common::$ database ->prepare ('INSERT INTO `packet_used_by` (`id`, `bnet_product_id`) VALUES (?, ?); ' );
986
- foreach ($ value as $ v )
987
- {
988
- $ r = $ q ->execute ([(int ) $ this ->id , (int ) $ v ]);
989
- if (!$ r )
990
- {
991
- Common::$ database ->rollBack ();
992
- return $ r ;
993
- }
994
- }
995
-
996
- Common::$ database ->commit ();
997
- }
998
- catch (PDOException $ e )
996
+ foreach ($ value as $ v )
999
997
{
1000
- Common::$ database ->rollBack ();
1001
- throw $ e ;
998
+ $ used_by [] = ($ v instanceof Product ? $ v : new Product ($ v ));
1002
999
}
1000
+
1001
+ $ this ->used_by = $ used_by ;
1003
1002
}
1004
1003
1005
1004
/**
0 commit comments