22namespace BNETDocs \Libraries ;
33
44use \BNETDocs \Libraries \Exceptions \PacketNotFoundException ;
5+ use \BNETDocs \Libraries \Exceptions \ProductNotFoundException ;
56use \BNETDocs \Libraries \IDatabaseObject ;
67use \BNETDocs \Libraries \Packet \Application as ApplicationLayer ;
78use \BNETDocs \Libraries \Packet \Transport as TransportLayer ;
9+ use \BNETDocs \Libraries \Product ;
810use \BNETDocs \Libraries \User ;
911use \CarlBennett \MVC \Libraries \Common ;
1012use \CarlBennett \MVC \Libraries \DatabaseDriver ;
@@ -116,6 +118,7 @@ public function allocate()
116118 $ this ->setPacketId (0 );
117119 $ this ->setRemarks ('' );
118120 $ this ->setTransportLayerId (self ::DEFAULT_TRANSPORT_LAYER_ID );
121+ $ this ->setUsedBy ([]);
119122
120123 if (is_null ($ id )) return ;
121124
@@ -154,10 +157,26 @@ public function allocate()
154157 throw new UnexpectedValueException (sprintf ('packet id: %d not found ' , $ id ));
155158 }
156159
157- $ r = $ q ->fetchObject ();
160+ $ o = $ q ->fetchObject ();
158161 $ q ->closeCursor ();
159162
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 );
161180 }
162181
163182 /**
@@ -182,6 +201,8 @@ protected function allocateObject(StdClass $value)
182201 $ this ->setRemarks ($ value ->packet_remarks );
183202 $ this ->setTransportLayerId ($ value ->packet_transport_layer_id );
184203 $ this ->setUserId ($ value ->user_id );
204+ if (!isset ($ value ->used_by )) throw new \RuntimeException ();
205+ $ this ->setUsedBy ($ value ->used_by );
185206 }
186207
187208 /**
@@ -225,9 +246,7 @@ public function commit()
225246 `packet_remarks` = :r,
226247 `packet_transport_layer_id` = :tr_id,
227248 `user_id` = :uid
228- ;
229- SELECT LAST_INSERT_ID();
230- ' );
249+ ; ' );
231250
232251 $ created_datetime = $ this ->created_datetime ->format (self ::DATE_SQL );
233252
@@ -252,10 +271,29 @@ public function commit()
252271 $ r = $ q ->execute ();
253272 if (!$ r ) return $ r ;
254273
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+ }
257296
258- $ q ->closeCursor ();
259297 return $ r ;
260298 }
261299
@@ -342,32 +380,17 @@ public static function getPacketsByLastEdited(int $count)
342380 Common::$ database = DatabaseDriver::getDatabaseObject ();
343381 }
344382
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
362385 ));
363386
364387 $ r = $ q ->execute ();
365388 if (!$ r ) return $ r ;
366389
367390 $ r = [];
368- while ($ row = $ q ->fetch (PDO ::FETCH_OBJ ))
391+ while ($ row = $ q ->fetch (PDO ::FETCH_NUM ))
369392 {
370- $ r [] = new self ($ row );
393+ $ r [] = new self ($ row[ 0 ] );
371394 }
372395
373396 $ q ->closeCursor ();
@@ -964,42 +987,18 @@ public function setTransportLayerId(int $value)
964987 /**
965988 * Sets the products this Packet is used by.
966989 *
967- * @param array $value The set of ProductId integers.
990+ * @param array $value The set of Product objects, or Product::$id integers.
968991 */
969992 public function setUsedBy (array $ value )
970993 {
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 = [];
984995
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 )
999997 {
1000- Common::$ database ->rollBack ();
1001- throw $ e ;
998+ $ used_by [] = ($ v instanceof Product ? $ v : new Product ($ v ));
1002999 }
1000+
1001+ $ this ->used_by = $ used_by ;
10031002 }
10041003
10051004 /**
0 commit comments