promo.class.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require_once("connector.class.php");
  3. class Promo
  4. {
  5. private $id_promo;
  6. private $libelle;
  7. function __construct($id)
  8. {
  9. $bdd = new Connector();
  10. $promo = $bdd->Select("*", "promo", array(
  11. "where" => array(
  12. array("id_promo", "=", $id)
  13. )
  14. ));
  15. if($promo == NULL)
  16. {
  17. throw new LengthException("La promo n'existe pas");
  18. }
  19. $promo = $promo[0];
  20. $this->id_promo = $promo["id_promo"];
  21. $this->libelle = $promo["libelle"];
  22. }
  23. public static function getAll()
  24. {
  25. $bdd = new Connector();
  26. return $bdd->Select("*", "promo");
  27. }
  28. /**
  29. * @return mixed
  30. */
  31. public function getIdPromo()
  32. {
  33. return $this->id_promo;
  34. }
  35. /**
  36. * @return mixed
  37. */
  38. public function getLibelle()
  39. {
  40. return $this->libelle;
  41. }
  42. /**
  43. * @param mixed $id_promo
  44. */
  45. public function setIdPromo($id_promo)
  46. {
  47. $this->id_promo = $id_promo;
  48. }
  49. /**
  50. * @param mixed $libelle
  51. */
  52. public function setLibelle($libelle)
  53. {
  54. $this->libelle = $libelle;
  55. }
  56. function write()
  57. {
  58. $bdd = new Connector();
  59. $promo = $bdd->Select("*", "promo", array(
  60. "where" => array(
  61. array("id_promo", "=", $this->id_promo)
  62. )
  63. ));
  64. if(!$promo)
  65. {
  66. throw new UnexpectedValueException("La promo n'existe plus en base de données");
  67. }
  68. $promo = $promo[0];
  69. $attrs = get_object_vars($this);
  70. $toUpdate = array();
  71. foreach ($attrs as $key => $value) {
  72. if ($value != $promo[$key]) {
  73. $toUpdate[$key] = $value;
  74. }
  75. }
  76. $bdd->Update("promo", array(
  77. "set" => $toUpdate,
  78. "where" => array(array("id_promo", "=", $this->id_promo))
  79. ));
  80. }
  81. function erase()
  82. {
  83. $bdd = new Connector();
  84. $bdd->Delete("promo", array(array("id_promo", "=", $this->id_promo)));
  85. }
  86. public static function addPromo($promo)
  87. {
  88. $bdd = new Connector();
  89. $bdd->Insert("promo", array(
  90. "id_promo" => $promo["id"],
  91. "libelle" => $promo["libelle"]
  92. ));
  93. }
  94. }