ElijahLoop 0 Posted February 17 Share Posted February 17 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 post Share on other sites
gw1500se 62 Posted February 17 Share Posted February 17 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 post Share on other sites
Barand 1,632 Posted February 17 Share Posted February 17 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 post Share on other sites
gw1500se 62 Posted February 17 Share Posted February 17 I'm a belt and suspenders kind of guy. Quote Link to post Share on other sites
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.