コード例 #1
0
ファイル: XS.php プロジェクト: emachiels/SqlXS
 /**
  * Inserts a new row in the table
  *
  * @param array $data The [fields] => values
  * @throws XsException
  */
 public static function insert(array $data)
 {
     foreach ($data as $f => $v) {
         $fields[] = QueryBuilder::fieldName($f);
         // Make sure type is correct
         $type = self::sqlXS()->getType($f);
         if ($type !== null && !is_scalar($v)) {
             $v = $v->id();
         }
         $values[] = XsConfiguration::getPDO()->quote($v);
     }
     try {
         XsConfiguration::getPDO()->query('INSERT INTO ' . QueryBuilder::tableName(self::sqlXS()->getTable()) . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')');
         return self::byID(XsConfiguration::getPDO()->lastInsertId());
     } catch (PDOException $ex) {
         throw new XsException('Could not insert row. PDO Error: ' . $ex->getMessage());
     }
 }
コード例 #2
0
ファイル: QueryBuilder.php プロジェクト: emachiels/SqlXS
 public function __toString()
 {
     // Work around escaped paremeters
     $query = str_replace('\\@', '@\\', $this->query);
     // Loop through all the withs and replcae them with the field name
     foreach ($this->with as $key => $with) {
         // Check whether the key is valid (an integer or a :string without space)
         if (is_int($key) || is_string($key) && strlen($key) > 0 && $key[0] == ':' && !preg_match('/\\s/', $key)) {
             // If the field doesnt specify a table already, add it
             $field = is_array($with) ? $with : [$this->table, $with];
             // Replace the %parameter
             $query = str_replace('@' . $key, QueryBuilder::fieldName($field), $query);
         } else {
             throw new Exception('Subselect with-key ' . strip_tags($key) . ' is not valid. It must either be an integer or a spaceless string prefixed with a colon (:)');
         }
     }
     // Add the @table param
     $query = str_replace('@table', QueryBuilder::tableName($this->table), $query);
     // Put back original @ signs
     return str_replace('@\\', '@', $query);
 }