public function save()
{
$this->db->beginTransaction();
try {
$dataAttributes = get_object_vars($this->model);
$originalIdPath = $this->db->fetchOne("SELECT idPath FROM tags WHERE id = ?", $this->model->getId());
$data = [];
foreach ($dataAttributes as $key => $value) {
if (in_array($key, $this->getValidTableColumns("tags"))) {
$data[$key] = $value;
}
}
$this->db->insertOrUpdate("tags", $data);
$lastInsertId = $this->db->lastInsertId();
if (!$this->model->getId() && $lastInsertId) {
$this->model->setId($lastInsertId);
}
//check for id-path and update it, if path has changed -> update all other tags that have idPath == idPath/id
if ($originalIdPath && $originalIdPath != $this->model->getIdPath()) {
$this->db->query("UPDATE tags SET idPath = REPLACE(idPath, ?, ?) WHERE idPath LIKE ?;", [$originalIdPath, $this->model->getIdPath(), $originalIdPath . $this->model->getId() . "/%"]);
}
$this->db->commit();
return true;
} catch (\Exception $e) {
$this->db->rollBack();
throw $e;
}
}