Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]MySQL setters i getters
sadistic_son
post
Post #1





Grupa: Zarejestrowani
Postów: 1 495
Pomógł: 245
Dołączył: 1.07.2009
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Cześć.
Walczę z kolejnym wymogiem projektu nad którym siedzę.
"Logika MySQL powinna być obsługiwana przez obiekty z właściwościami, zamiast bezpośrednich wartości kolumn. Proszę użyć setters i getters aby to osiągnąć oraz nie zapomnieć użyć ich do zapisu i wyświetlania logiki."
Póki co to nie użyłem setter/getter. Pokażcie proszę na przykładzie mojego poniższego kodu jak to powinno być zastosowane.

Klasa Produkt
  1. class Product
  2. {
  3.  
  4. public $productName;
  5. public $sku;
  6. public $price;
  7. public $productType;
  8. public function __construct()
  9. {
  10. $this->productName = isset($_POST['name']) ? (string) $_POST['name'] : null;
  11. $this->sku = isset($_POST['sku']) ? (string) $_POST['sku'] : null;
  12. $this->price = isset($_POST['price']) ? (float) $_POST['price'] : null;
  13. $this->productType = isset($_POST['productType']) ? (string) $_POST['productType'] : null;
  14. }
  15.  
  16. function ensureNoEmpytValues()
  17. {
  18. if (empty($this->productName) || empty($this->sku) || empty($this->price) || empty($this->productType))
  19. return false;
  20. return true;
  21. }
  22. }


Kawałek klasy ProductAdd z metodą wrzucającą do bazy:
  1. <?php
  2. class ProductAdd extends Product
  3. {
  4. public function insertNewProduct()
  5. {
  6.  
  7. if ($this->ensureNoEmpytValues() === false)
  8. return false;
  9.  
  10. $db = new DB();
  11.  
  12. $sku = $this->sku;
  13. $productName = $this->productName;
  14. $price = $this->price;
  15. $selectedType = $this->productType;
  16.  
  17.  
  18. $properties = $this->groupFullProperties();
  19.  
  20. foreach ($properties[$selectedType] as $propertyData) {
  21. $postIndex = $propertyData['property'];
  22. if (empty($_POST[$postIndex]))
  23. return false;
  24. }
  25. $queryInsertProduct = "INSERT INTO `product` (`id`, `sku`, `name`, `price`, `type`) VALUES (null, :sku, :productName , :price, :selectedType)";
  26. $db->query($queryInsertProduct);
  27. $db->bind(':sku', $sku);
  28. $db->bind(':productName', $productName);
  29. $db->bind(':price', $price);
  30. $db->bind(':selectedType', $selectedType);
  31.  
  32. $db->execute();
  33.  
  34. $lastProductId = $db->lastInsertId();
  35.  
  36. foreach ($properties[$selectedType] as $propertyData) {
  37. $propertyId = (int) $propertyData['id'];
  38. $productValue = $_POST[(string) $propertyData['property']];
  39.  
  40. $queryInsertProductProperty = "INSERT INTO `product_property` (`id`, `product_id`, `property_id`, `value`) VALUES (null, $lastProductId , $propertyId, :productValue )";
  41. $db->query($queryInsertProductProperty);
  42. $db->bind(':productValue', $productValue);
  43.  
  44. $db->execute();
  45. }
  46. }
  47. }


Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
sadistic_son
post
Post #2





Grupa: Zarejestrowani
Postów: 1 495
Pomógł: 245
Dołączył: 1.07.2009
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Product:
  1. <?php
  2.  
  3. class Product
  4. {
  5.  
  6. private string $productName;
  7. private string $sku;
  8. private float $price;
  9. private string $productType;
  10. public function __construct()
  11. {
  12. $this->productName = isset($_POST['name']) ? (string) $_POST['name'] : null;
  13. $this->sku = isset($_POST['sku']) ? (string) $_POST['sku'] : null;
  14. $this->price = isset($_POST['price']) ? (float) $_POST['price'] : null;
  15. $this->productType = isset($_POST['productType']) ? (string) $_POST['productType'] : null;
  16. }
  17.  
  18. function ensureNoEmpytValues()
  19. {
  20. if (empty($this->productName) || empty($this->sku) || empty($this->price) || empty($this->productType))
  21. return false;
  22. return true;
  23. }
  24. }


Oraz ProductAdd:
  1. <?php
  2.  
  3. namespace nameOne;
  4. use Product;
  5.  
  6. class ProductAdd extends Product
  7. {
  8. private DB $db;
  9.  
  10. public function __construct(DB $db){
  11. $this->db = $db;
  12. // parent::__construct();
  13. }
  14.  
  15. public function listProperties()
  16. {
  17. $properties = $this->getDistinctProperties();
  18. return $properties;
  19. }
  20.  
  21. private function getDistinctProperties()
  22. {
  23. // $db = new DB();
  24. $this->db->query("SELECT DISTINCT type FROM property ORDER BY type ASC");
  25. $results = $this->db->resultSet();
  26. return $results;
  27. }
  28.  
  29. public function listFullProperties()
  30. {
  31. $properties = $this->getFullProperties();
  32. return $properties;
  33. }
  34. private function getFullProperties()
  35. {
  36. // $db = new DB();
  37. $this->db->query("SELECT * FROM property ORDER BY type ASC");
  38. $results = $this->db->resultSet();
  39. return $results;
  40. }
  41.  
  42. public function groupFullProperties(): array
  43. {
  44. $properties = array();
  45. foreach ($this->getFullProperties() as $row) {
  46. $property = $row['property'];
  47.  
  48. $newProperty = $row['type'];
  49. if (!isset($properties[$newProperty])) {
  50. $properties[$newProperty] = array();
  51. }
  52. $properties[$newProperty][] = [
  53. 'id' => $row['id'],
  54. 'property' => $property,
  55. 'label' => $row['label'],
  56. 'description' => $row['description']
  57.  
  58. ];
  59.  
  60. }
  61. return $properties;
  62. }
  63.  
  64. public function checkExistingSKU(string $skuToCheck)
  65. {
  66. // $db = new DB();
  67. $this->db->query("SELECT sku FROM product WHERE sku = :sku");
  68. $this->db->bind(':sku', $skuToCheck);
  69.  
  70. if ($this->db->single()) {
  71. return true;
  72. }
  73. return false;
  74. }
  75.  
  76.  
  77. public function insertNewProduct()
  78. {
  79.  
  80. /* to na razie jest zakomentowane żebym w ogóle widział jakie są błędy*/
  81. // if ($this->ensureNoEmpytValues() === false)
  82. // return false;
  83.  
  84.  
  85.  
  86.  
  87. $sku = $this->sku;
  88. $productName = $this->productName;
  89. $price = $this->price;
  90. $selectedType = $this->productType;
  91.  
  92.  
  93.  
  94. $properties = $this->groupFullProperties();
  95.  
  96.  
  97. foreach ($properties[$selectedType] as $propertyData) {
  98. $postIndex = $propertyData['property'];
  99. if (empty($_POST[$postIndex]))
  100. return false;
  101. }
  102. $queryInsertProduct = "INSERT INTO `product` (`id`, `sku`, `name`, `price`, `type`) VALUES (null, :sku, :productName , :price, :selectedType)";
  103. $this->db->query($queryInsertProduct);
  104. $this->db->bind(':sku', $sku);
  105. $this->db->bind(':productName', $productName);
  106. $this->db->bind(':price', $price);
  107. $this->db->bind(':selectedType', $selectedType);
  108.  
  109. $this->db->execute();
  110.  
  111. $lastProductId = $this->db->lastInsertId();
  112.  
  113. foreach ($properties[$selectedType] as $propertyData) {
  114.  
  115. $propertyId = (int) $propertyData['id'];
  116. $productValue = $_POST[(string) $propertyData['property']];
  117.  
  118. $queryInsertProductProperty = "INSERT INTO `product_property` (`id`, `product_id`, `property_id`, `value`) VALUES (null, $lastProductId , $propertyId, :productValue )";
  119. $this->db->query($queryInsertProductProperty);
  120. $this->db->bind(':productValue', $productValue);
  121.  
  122. $this->db->execute();
  123. }
  124.  
  125.  
  126. }
  127.  
  128. }
  129.  
  130. ?>

Przyczepia się że nie wie co to jest:
$sku = $this->sku;
$productName = $this->productName;
$price = $this->price;
$selectedType = $this->productType;

Jak w product zmienię na public to za to przyczepia się że Product::$sku must not be accessed before initialization

Ten post edytował sadistic_son 12.01.2023, 15:26:52
Go to the top of the page
+Quote Post

Posty w temacie
- sadistic_son   [PHP]MySQL setters i getters   10.01.2023, 14:47:37
- - nospor   Ja nie wiem na kiedy masz to zrobic, ale warto by ...   10.01.2023, 15:29:26
|- - sadistic_son   Cytat(nospor @ 10.01.2023, 15:29:26 )...   10.01.2023, 16:52:15
- - nospor   No dobra, nie pozostalo w takim razie nic innego j...   10.01.2023, 16:55:20
|- - sadistic_son   Cytat(nospor @ 10.01.2023, 16:55:20 )...   12.01.2023, 12:18:56
- - nospor   CytatCzy jednak da się na to przymknąć oko u kogoś...   12.01.2023, 12:27:05
- - sadistic_son   W chwili obecnej moja klasa DB wygląda tak: [PHP] ...   12.01.2023, 12:46:01
- - nospor   CytatTeraz na początku ProductList.view.php tworze...   12.01.2023, 12:50:37
|- - sadistic_son   Cytat(nospor @ 12.01.2023, 12:50:37 )...   12.01.2023, 13:06:46
- - nospor   CytatNie wiem czy chcę się tutaj dzielić ProductAd...   12.01.2023, 13:14:44
- - sadistic_son   Ok, to ProductList wkleiłem wyżej a tutaj najwięks...   12.01.2023, 13:18:36
- - nospor   Jak pisalem wczesniej: nie $productObject = n...   12.01.2023, 13:27:24
|- - sadistic_son   Cytat(nospor @ 12.01.2023, 13:27:24 )...   12.01.2023, 14:05:55
- - nospor   napisz te setter dla Product tylko. CytatSerio je...   12.01.2023, 14:19:11
|- - sadistic_son   Cytat(nospor @ 12.01.2023, 14:19:11 )...   12.01.2023, 14:53:00
- - nospor   CytatCoś gdzieś po drodze spieprzyłem i nie potraf...   12.01.2023, 14:55:21
- - sadistic_son   Nic jeszcze nie zrobiłem, bo od godziny (odkąd pop...   12.01.2023, 15:07:40
- - nospor   Kurcze, ale ty nadal dziedziczysz po Produkt. A p...   12.01.2023, 15:08:48
|- - sadistic_son   Cytat(nospor @ 12.01.2023, 15:08:48 )...   12.01.2023, 15:14:03
- - nospor   Pokaz jak wyglada teraz ProductAdd i Product   12.01.2023, 15:17:59
- - sadistic_son   Product:[PHP] pobierz, plaintext <?php cla...   12.01.2023, 15:21:31
- - nospor   zmien private string $productName; na protec...   12.01.2023, 15:25:20
- - sadistic_son   Niestety to samo co wczesniej -Fatal error: Uncaug...   12.01.2023, 15:29:47
- - nospor   Cannot assign null to property Product::$prod...   12.01.2023, 15:32:11
- - sadistic_son   No tak, przypisywałem null do stringa, to jasne że...   12.01.2023, 15:56:05
- - nospor   Klase Produkt zostala ci zaproponowana wczesniej j...   12.01.2023, 16:01:51
- - sadistic_son   Ok, zaczynam chwytać. Nigdzie w żadnych artykułach...   12.01.2023, 16:05:54
- - nospor   Na te chwile moze.   12.01.2023, 16:08:24
- - sadistic_son   ok. To klasa Product nie ma mieć właściwie żadnych...   12.01.2023, 16:11:43
- - nospor   tak I jesli to juz bedzie oddzielna klasa i Produ...   12.01.2023, 16:14:00
- - sadistic_son   Ok, to to mi sporo wyjaśnia. Tego elementu mi chyb...   13.01.2023, 09:59:43
- - nospor   No obiekt, ale konkretnie obiekt PDO wiec pri...   13.01.2023, 10:04:00
|- - sadistic_son   Cytat(nospor @ 13.01.2023, 10:04:00 )...   13.01.2023, 10:15:34
- - nospor   Starasz sie o te nowa robote, ona wymaga samodziel...   13.01.2023, 10:20:17
|- - sadistic_son   Cytat(nospor @ 13.01.2023, 10:20:17 )...   13.01.2023, 10:33:15
- - viking   Poczytaj sobie https://prophp.pl/advice/show/14/ja...   13.01.2023, 10:20:55
- - nospor   $this->db->bind(':sku', $s...   13.01.2023, 10:44:30
- - sadistic_son   To z sku to tylko przykład wywołania metody bind. ...   13.01.2023, 10:49:29
- - nospor   W przykladach tutaj co podales zawsze jest NULL. A...   13.01.2023, 10:51:44
- - sadistic_son   O, i teraz się zrozumieliśmy. Dzięki za odpowiedź ...   13.01.2023, 10:59:04
- - nospor   Generalnie dobrze, ale nie tu $sku = $p...   13.01.2023, 11:00:53
- - sadistic_son   Tak tak, to dopisałem w poprzednim poście już jako...   13.01.2023, 11:29:25
- - nospor   Nie, konstruktor to przypadek szczegolny   13.01.2023, 11:31:13
- - sadistic_son   A co ma wskazywać typ zwracanych danych w przypadk...   13.01.2023, 11:34:10
- - viking   Jeszcze to sobie poczytaj: https://designpatternsp...   13.01.2023, 11:34:34
- - nospor   CytatA co ma wskazywać typ zwracanych danych w prz...   13.01.2023, 11:41:18
|- - Salvation   Cytat(nospor @ 13.01.2023, 11:41:18 )...   13.01.2023, 11:49:04
- - nospor   Jeszcze ani razu nie uzylem UNION w moich projekta...   13.01.2023, 11:59:36
- - sadistic_son   Dobra, rozumiem. ale w takim razie czemu np TUTA...   13.01.2023, 12:09:50
- - nospor   Metody PHP istnieja od wiekow, kiedy byla jeszcze ...   13.01.2023, 12:32:29
- - sadistic_son   Czyli bezczelnie przerabiamy string z PDO na int. ...   13.01.2023, 12:36:25
- - nospor   lastInsertId z PDO to metoda uniwersalna. Rozne si...   13.01.2023, 12:47:48
- - sadistic_son   Rozumiem. Dzięki. A wracając do setter/getter.....   13.01.2023, 12:58:57
- - nospor   listProducts wydaje sie zbedne, tak. Zas z ta log...   13.01.2023, 13:35:46
- - viking   W sumie zwracał CI już na to uwagę ale po co wprow...   13.01.2023, 13:52:53
|- - sadistic_son   Cytat(viking @ 13.01.2023, 13:52:53 )...   13.01.2023, 14:06:16
- - nospor   No pewnie listGroupedProducts powinna juz zwracac ...   13.01.2023, 14:27:13
- - sadistic_son   No dobra, to rozszerzyłem klasę Product o $pr...   13.01.2023, 15:10:55
- - nospor   array('name' => $productObject-...   13.01.2023, 15:21:00
- - sadistic_son   RE: [PHP]MySQL setters i getters   13.01.2023, 15:34:58
- - nospor   [PHP] pobierz, plaintext   public func...   13.01.2023, 15:42:28
|- - sadistic_son   Cytat(nospor @ 13.01.2023, 15:42:28 )...   13.01.2023, 16:05:23
- - nospor   masz ddoac a nie nadpisac. .... private array ...   13.01.2023, 16:17:05
- - sadistic_son   No tak. Oczywiste... Ech, czas na dłuższą przerwę...   13.01.2023, 16:19:06
- - nospor   no chyba tak Tylko ne tworz takich cudow ...   13.01.2023, 18:26:36
|- - sadistic_son   Cytat(nospor @ 13.01.2023, 18:26:36 )...   23.01.2023, 11:32:36
- - nospor   Oj chyba sie zgubilem. O co dokladnie pytasz?   23.01.2023, 14:29:26
- - sadistic_son   Chodzi mi o to, że: W formularzu mam pole select ...   23.01.2023, 15:38:19
- - nospor   Poprostu do metody przekaz caly $_POST i po s...   23.01.2023, 15:49:46
- - sadistic_son   No ok, ale co w tej klasie Request miałoby być? Ni...   23.01.2023, 17:14:43
- - viking   Dawałem ci wcześniej linki do klas na github. Nie ...   23.01.2023, 17:55:01
- - nospor   CytatNie napisałem jej, bo kiedy dopytywałem o czy...   24.01.2023, 09:50:03


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 11.10.2025 - 09:59