|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace app\models; |
| 4 | + |
| 5 | +use app\core\Application; |
| 6 | +use app\core\DbModel; |
| 7 | + |
| 8 | + |
| 9 | +class Promotion extends DbModel |
| 10 | +{ |
| 11 | + |
| 12 | + public function tableName(): string |
| 13 | + { |
| 14 | + return 'promotion'; |
| 15 | + } |
| 16 | + |
| 17 | + public function primaryKey(): string |
| 18 | + { |
| 19 | + return 'promotion_id'; |
| 20 | + } |
| 21 | + |
| 22 | + public function rules(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + 'admin_id' => [self::RULE_REQUIRED], |
| 26 | + 'descriptioin' => [self::RULE_REQUIRED], |
| 27 | + 'price' => [self::RULE_REQUIRED], |
| 28 | + 'created_at' => [self::RULE_REQUIRED], |
| 29 | + 'updated_at' => [self::RULE_REQUIRED], |
| 30 | + |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public function updateRules(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + public function attributes(): array |
| 43 | + { |
| 44 | + return [ |
| 45 | + 'admin_id', |
| 46 | + 'description', |
| 47 | + 'price', |
| 48 | + 'media', |
| 49 | + 'created_at', |
| 50 | + 'updated_at' |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + public static function getAllPromotions() |
| 56 | + { |
| 57 | + $sql = "SELECT * FROM promotion"; |
| 58 | + $stmt = (new Promotion)->prepare($sql); |
| 59 | + $stmt->execute(); |
| 60 | + $promotion = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 61 | + return $promotion; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public static function insert_promotion($desc, $create, $price, $update, $admin_id = 1) |
| 66 | + { |
| 67 | + $sql = "INSERT INTO promotion |
| 68 | + (description, created_at, price, updated_at,admin_id) |
| 69 | + VALUES (:desc, :created, :price, :updated,:admin)"; |
| 70 | + |
| 71 | + $stmt = (new Promotion)->prepare($sql); |
| 72 | + $stmt->bindParam(':desc', $desc); |
| 73 | + $stmt->bindParam(':created', $create); |
| 74 | + $stmt->bindParam(':price', $price); |
| 75 | + $stmt->bindParam(':updated', $update); |
| 76 | + $stmt->bindParam(':admin', $admin_id); |
| 77 | + |
| 78 | + return $stmt->execute(); |
| 79 | + } |
| 80 | + |
| 81 | + public static function updatePromotion($id, $desc, $price, $update) |
| 82 | + { |
| 83 | + $sql = "UPDATE promotion |
| 84 | + SET description = :desc, |
| 85 | + price = :price, |
| 86 | + updated_at = :updated |
| 87 | + WHERE promotion_id = :id"; |
| 88 | + |
| 89 | + $stmt = (new Promotion)->prepare($sql); |
| 90 | + $stmt->bindParam(':id', $id); |
| 91 | + $stmt->bindParam(':desc', $desc); |
| 92 | + $stmt->bindParam(':price', $price); |
| 93 | + $stmt->bindParam(':updated', $update); |
| 94 | + |
| 95 | + return $stmt->execute(); |
| 96 | + } |
| 97 | + |
| 98 | + public static function deletePromotion($id) |
| 99 | + { |
| 100 | + $sql = "DELETE FROM promotion WHERE promotion_id = :id"; |
| 101 | + |
| 102 | + $stmt = (new Promotion)->prepare($sql); |
| 103 | + $stmt->bindParam(':id', $id); |
| 104 | + |
| 105 | + return $stmt->execute(); |
| 106 | + } |
| 107 | +} |
0 commit comments