1
+ <?php
2
+
3
+ namespace app \models ;
4
+
5
+ use app \core \DbModel ;
6
+
7
+ class Product extends DbModel
8
+ {
9
+ public int $ ser_cen_id ;
10
+ public string $ description = '' ;
11
+ public float $ price ;
12
+ public string $ media ;
13
+ public ?string $ created_at = null ;
14
+ public ?string $ updated_at = null ;
15
+
16
+ public function tableName (): string
17
+ {
18
+ return 'product ' ;
19
+ }
20
+
21
+ public function attributes (): array
22
+ {
23
+ return ['ser_cen_id ' , 'description ' , 'price ' , 'media ' ];
24
+ }
25
+ public function primaryKey (): string
26
+ {
27
+ return 'product_id ' ;
28
+ }
29
+
30
+ // public function loadData($data)
31
+ // {
32
+ // parent::loadData($data); // TODO: Change the autogenerated stub
33
+ // }
34
+
35
+ public function save ()
36
+ {
37
+ $ this ->media = $ _FILES ['media ' ]['name ' ];
38
+ move_uploaded_file ($ _FILES ['media ' ]['tmp_name ' ], 'assets/uploads/ ' . $ this ->media );
39
+ return parent ::save ();
40
+ }
41
+
42
+ public static function getAllProducts (): array
43
+ {
44
+ $ sql = 'SELECT p.*, s.name AS seller_name
45
+ FROM product p
46
+ JOIN service_center s ON p.ser_cen_id = s.ser_cen_id
47
+ ORDER BY p.created_at DESC ' ;
48
+ $ stmt = (new Product )->prepare ($ sql );
49
+ $ stmt ->execute ();
50
+ return $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
51
+ }
52
+ public function editProduct (): bool
53
+ {
54
+ $ tableName = self ::tableName ();
55
+ $ stmt = self ::prepare ("
56
+ UPDATE $ tableName
57
+ SET description = :description, price = :price, media = :media, updated_at = NOW()
58
+ WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
59
+ " );
60
+ $ stmt ->bindValue (':description ' , $ this ->description );
61
+ $ stmt ->bindValue (':price ' , $ this ->price );
62
+ $ stmt ->bindValue (':media ' , $ this ->media );
63
+ $ stmt ->bindValue (':product_id ' , $ this ->product_id );
64
+ $ stmt ->bindValue (':ser_cen_id ' , $ this ->ser_cen_id );
65
+ return $ stmt ->execute ();
66
+ }
67
+ public function deleteProduct (int $ product_id , int $ ser_cen_id ): bool
68
+ {
69
+ $ tableName = self ::tableName ();
70
+ $ statement = self ::prepare ("
71
+ DELETE FROM $ tableName
72
+ WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
73
+ " );
74
+ $ statement ->bindValue (':product_id ' , $ product_id );
75
+ $ statement ->bindValue (':seller_id ' , $ ser_cen_id );
76
+ return $ statement ->execute ();
77
+ }
78
+ public function productRules (): array
79
+ {
80
+ return [
81
+ 'seller_id ' => [self ::RULE_REQUIRED ],
82
+ 'name ' => [self ::RULE_REQUIRED , [self ::RULE_MAX , 'max ' => 255 ]],
83
+ 'description ' => [self ::RULE_REQUIRED , [self ::RULE_MAX , 'max ' => 1000 ]],
84
+ 'price ' => [self ::RULE_REQUIRED ]
85
+ ];
86
+ }
87
+ }
88
+
89
+ ?>
0 commit comments