has("containers", ["containerid" => $containerid])) { $this->exists = true; $info = $database->get("containers", ["barcode"], ["containerid" => $containerid]); $this->barcode = $info["barcode"]; } else { $this->exists = false; } } public function save() { global $database; if ($database->has("containers", ["containerid" => $this->containerid])) { $database->update("containers", [ "barcode" => $this->barcode ], ["containerid" => $this->containerid]); } else { $database->insert("containers", ["barcode" => $this->barcode]); $this->containerid = $database->id(); $this->exists = true; } $database->action(function ($database) { $database->delete("container_contents", ["containerid" => $this->containerid]); foreach ($this->machines as $id) { $database->insert("container_contents", [ "machineid" => $id, "containerid" => $this->containerid ]); } foreach ($this->components as $id) { $database->insert("container_contents", [ "componentid" => $id, "containerid" => $this->containerid ]); } foreach ($this->others as $id) { $database->insert("container_contents", [ "otherid" => $id, "containerid" => $this->containerid ]); } }); } public function getMachineIDs(): array { return $this->machines; } public function getComponentIDs(): array { return $this->components; } public function getOtherIDs(): array { return $this->others; } public function addMachine($machineid) { global $database; if ($database->has("machines", ["machineid" => $machineid])) { $this->machines[] = $machineid; } else { throw new Exception("No machine with ID $machineid exists."); } } public function addComponent($componentid) { global $database; if ($database->has("components", ["compid" => $componentid])) { $this->components[] = $componentid; } else { throw new Exception("No component with ID $componentid exists."); } } public function addOtherID($id) { $this->others[] = $id; } public function rmMachine($machineid) { foreach (array_keys($this->machines, $machineid) as $id) { unset($this->machines[$id]); } } public function rmComponent($componentid) { foreach (array_keys($this->components, $componentid) as $id) { unset($this->components[$id]); } } public function rmOtherID($oid) { foreach (array_keys($this->others, $oid) as $id) { unset($this->others[$id]); } } }