Beispiel #1
0
 /**
  * Saves the model, either inserting (no id) or updating (id)
  *
  * @return mixed
  */
 public function save()
 {
     $array = $this->toArray();
     $query = $this->createQuery();
     $now = (new \DateTime())->format('Y-m-d H:i:s');
     if ($this->id) {
         $query->setAction('update');
         $query->addValue($this->getTableKey(), $this->id);
         foreach ($array as $key => $value) {
             $query->addValue($key, $value);
         }
         // Since it's an update, add updated_at if the model implements it
         if (property_exists($this, 'updated_at')) {
             $query->addValue('updated_at', $now);
             $this->updated_at = $now;
         }
     } else {
         $query->setAction('insert');
         foreach ($array as $key => $value) {
             if ($key !== $this->tableKey) {
                 $query->addValue($key, $value);
             }
         }
         // Since it's an insert, add created_at if the model implements it
         if (property_exists($this, 'created_at')) {
             $query->addValue('created_at', $now);
             $this->created_at = $now;
         }
     }
     $result = $this->database->query($query);
     if ($result && $query->getAction() === 'insert') {
         $this->id = $this->database->getInstance()->lastInsertId();
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Outputs the actual query for use, empty string if invalid/incomplete values given
  *
  * @return string
  */
 public function __toString()
 {
     // If there's no valid PDO instance, we can't quote so no query for you
     if (!$this->database->getInstance()) {
         return '';
     }
     $query = [];
     if ($this->action === 'select') {
         $query[] = "SELECT " . $this->select;
         $query[] = "FROM " . $this->getQuotedTableName();
         $query[] = $this->buildJoins();
         $query[] = $this->buildWheres();
         $query[] = $this->buildOrderBy();
         $query[] = $this->buildGroupBy();
         $query[] = $this->buildLimitOffset();
     } elseif ($this->action === 'delete') {
         $query[] = "DELETE FROM " . $this->getQuotedTableName();
         $query[] = $this->buildWheres();
     } elseif ($this->action === 'update') {
         $query[] = "UPDATE " . $this->getQuotedTableName();
         // now get the values
         if (count($this->values) > 0) {
             // Set the table values to defaults
             $tableKey = 'id';
             $tableKeyValue = null;
             $values = [];
             foreach ($this->values as $key => $value) {
                 // skip id, since we'll use that as a where condition
                 if ($key !== $this->tableKey) {
                     if ($value === null) {
                         $correctValue = 'NULL';
                     } else {
                         $correctValue = $this->database->quote($value);
                     }
                     // Quote the key
                     $key = $this->database->quoteIdentifier($key);
                     // Add key & value combo to the array
                     $values[] = $key . " = " . $correctValue;
                 } else {
                     $tableKey = $key;
                     $tableKeyValue = $value;
                 }
             }
             $query[] = "SET " . implode(', ', $values);
             $query[] = "WHERE " . $this->database->quoteIdentifier($tableKey);
             $query[] = " = " . $this->database->quote($tableKeyValue);
         } else {
             $query = [];
         }
     } elseif ($this->action === 'insert') {
         // set insert to the proper table
         $query[] = "INSERT INTO " . $this->getQuotedTableName();
         // now get the values
         if (count($this->values) > 0) {
             $keys = [];
             $values = [];
             foreach ($this->values as $key => $value) {
                 // Quote the key
                 $keys[] = $this->database->quoteIdentifier($key);
                 if ($value === null) {
                     $correctValue = 'NULL';
                 } else {
                     $correctValue = $this->database->quote($value);
                 }
                 $values[] = $correctValue;
             }
             $query[] = "(" . implode(', ', $keys) . ")";
             $query[] = "VALUES";
             $query[] = "(" . implode(', ', $values) . ")";
         } else {
             $query = [];
         }
     }
     // and now implode it into a nice string, if possible
     if (count($query) == 0) {
         return '';
     }
     // Now make it nice.
     $queryString = implode(' ', $query);
     $queryString = trim($queryString) . ';';
     // Since we got here, we've got a query to output
     return $queryString;
 }