diff options
| author | Malf Furious <m@lfurio.us> | 2015-12-18 00:34:15 -0500 | 
|---|---|---|
| committer | Malf Furious <m@lfurio.us> | 2015-12-18 00:34:15 -0500 | 
| commit | 2d674ddde9b02a5800e7b7004bc7453305e5862c (patch) | |
| tree | 8372e7693cb3608aac65e9b34e7fd04a8241efea /app/class | |
| parent | c31231740866fc31f9f40f9cf53555efec032291 (diff) | |
| download | scrott-2d674ddde9b02a5800e7b7004bc7453305e5862c.tar.gz scrott-2d674ddde9b02a5800e7b7004bc7453305e5862c.zip | |
+ Added saveObj function to Object class
Diffstat (limited to 'app/class')
| -rw-r--r-- | app/class/object.class.php | 94 | 
1 files changed, 93 insertions, 1 deletions
| diff --git a/app/class/object.class.php b/app/class/object.class.php index 4d00009..fb38ef7 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -46,7 +46,7 @@ abstract class Object extends Framework          $escdGuid = $this->db->esc($guid); -        /* Common fields */ +        /* Base fields */          $query = "SELECT * FROM `" . $this->table . "` WHERE `guid` = '" . $escdGuid . "'";          $result = $this->db->query($query)[0]; @@ -66,6 +66,98 @@ abstract class Object extends Framework                  $this->$col = $result[$col];          }      } + +    /* +     * Write this object to the database +     */ +    function saveObj() +    { +        if (isset($this->guid)) +        { +            /* Update Base */ +            $updateStr = ""; + +            foreach ($this->cols as $col) +            { +                if (!isset($this->$col)) +                    continue; + +                $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; +            } + +            if (strlen($updateStr) > 0) +            { +                $updateStr = substr($updateStr, 0, -2); // remove ", " from the end +                $query = "UPDATE `" . $this->table . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; +                $this->db->query($query); +            } + +            /* Update Child */ +            $updateStr = ""; + +            foreach ($this->childCols as $col) +            { +                if (!isset($this->$col)) +                    continue; + +                $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; +            } + +            if (strlen($updateStr) > 0) +            { +                $updateStr = substr($updateStr, 0, -2); // remove ", " from the end +                $query = "UPDATE `" . $this->childTable . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; +                $this->db->query($query); +            } +        } + +        else +        { +            $this->guid = $this->getNewGUID(); + +            /* Insert Base */ +            $colsStr = ""; +            $valsStr = ""; + +            foreach ($this->cols as $col) +            { +                if (!isset($this->$col)) +                    continue; + +                $colsStr .= "`" . $col . "`, "; +                $valsStr .= "'" . $this->db->esc($this->$col) . "', "; +            } + +            if (strlen($colsStr) > 0) +            { +                $colsStr = substr($colsStr, 0, -2); // remove ", " +                $valsStr = substr($valsStr, 0, -2); +                $query = "INSERT INTO `" . $this->table . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; +                $this->db->query($query); +            } + +            /* Insert Child */ +            $colsStr = ""; +            $valsStr = ""; + +            foreach ($this->childCols as $col) +            { +                if (!isset($this->$col)) +                    continue; + +                $colsStr .= "`" . $col . "`, "; +                $valsStr .= "'" . $this->db->esc($this->$col) . "', "; +            } + +            if (strlen($colsStr) > 0) +            { +                $colsStr = substr($colsStr, 0, -2); // remove ", " +                $valsStr = substr($valsStr, 0, -2); +                $query = "INSERT INTO `" . $this->childTable . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; +                $this->db->query($query); +            } +        } +    }  }  ?> | 
