ElijahLoop Posted February 17, 2021 Share Posted February 17, 2021 Hi everyone. Please help me please. I'm just learning PHP. There was a task, I completed it, but they sent a response that it needs to be modified. You need to make changes: "When saving data, you need to take into account the keys of the new data and the data stored in the database(file), if the value with the key in the database(file) already exists, then you need to replace its value with a new one." and "The FileBox and DbBox classes should be implemented in such a way that you can not create more than one instance of each of the classes." Googled and came across what i need to apply the singleton pattern. <?php interface Box { public function setData($key, $value); public function getData($key); public function save(); public function load(); } abstract class BoxAbstract implements Box { protected $data = []; public function setData($key, $value) { $this->data[$key] = $value; } public function getData($key) { return $this->data[$key] ?? null; } public abstract function save(); public abstract function load(); } class FileBox extends BoxAbstract { private $file; public function __construct($file) { $this->file = $file; } public function save() { file_put_contents($this->file, serialize($this->data)); } public function load() { $this->data = unserialize(file_get_contents($this->file)); } } class DbBox extends BoxAbstract { private $pdo; public function __construct(\PDO $pdo) { $this->pdo = $pdo; } public function save() { $this->pdo->beginTransaction(); $this->pdo->query('DELETE FROM box')->execute(); $stmt = $this->pdo->prepare("INSERT INTO box (key, value) VALUES (:key, :value)"); foreach ($this->data as $key => $value) { $stmt->bindValue(':key', $key); $stmt->bindValue(':value', serialize($value)); $stmt->execute(); } $this->pdo->commit(); } public function load() { $stmt = $this->pdo->query('SELECT key, value FROM box_items'); $data = []; while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $data[$row['key']] = unserialize($row['value']); } $this->data = $data; } } Quote Link to comment https://forums.phpfreaks.com/topic/312170-i-need-to-make-some-changes-to-the-code/ Share on other sites More sharing options...
gw1500se Posted February 17, 2021 Share Posted February 17, 2021 Define your primate keys as 'UNIQUE' in your schema. When you try to add a duplicate key, you will get an error. Handle that error by adding the new key. As for the class question, you are being asked to create a singleton class. Quote Link to comment https://forums.phpfreaks.com/topic/312170-i-need-to-make-some-changes-to-the-code/#findComment-1584574 Share on other sites More sharing options...
Barand Posted February 17, 2021 Share Posted February 17, 2021 1 hour ago, gw1500se said: Define your primate keys as 'UNIQUE' in your schema. Primary keys are unique identifiers for each entity and therefore, by definition, are already UNIQUE. (Or did you really mean monkeys? 🙂 ) Quote Link to comment https://forums.phpfreaks.com/topic/312170-i-need-to-make-some-changes-to-the-code/#findComment-1584576 Share on other sites More sharing options...
gw1500se Posted February 17, 2021 Share Posted February 17, 2021 I'm a belt and suspenders kind of guy. Quote Link to comment https://forums.phpfreaks.com/topic/312170-i-need-to-make-some-changes-to-the-code/#findComment-1584578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.