protected function prepareSqlAndArgs()
 {
     $sql = 'UPDATE ' . $this->db->quoteIdentifier($this->table->getTableName()) . ' SET ';
     $args = [];
     foreach ($this->fields as $name => $value) {
         $sql .= '' . $this->db->quoteIdentifier($name) . ' = ?, ';
         $args[] = $value;
     }
     $sql = substr($sql, 0, -2);
     list($whereSql, $whereArgs) = $this->getWhereSqlAndArgs();
     $sql .= $whereSql;
     $args = array_merge($args, $whereArgs);
     return [$sql, $args];
 }
 public function one()
 {
     $quotedFields = array_map(function ($field) {
         return $this->db->quoteIdentifier($field);
     }, array_keys($this->fields));
     $placeholders = array_map(function ($field) {
         return '?';
     }, array_values($this->fields));
     $fieldsSql = join(', ', $quotedFields);
     $placeholdersSql = join(', ', $placeholders);
     $sql = 'INSERT INTO ' . $this->db->quoteIdentifier($this->table->getTableName()) . ' (' . $fieldsSql . ') VALUES (' . $placeholdersSql . ')';
     if ($this->db instanceof AdapterPgsqlConnection) {
         $row = $this->table->sqlOne($sql . ' RETURNING *', array_values($this->fields));
     } elseif ($this->db instanceof AdapterMysqliConnection) {
         $this->db->execute($sql, array_values($this->fields));
         $row = $this->table->id($this->db->lastInsertId());
     } else {
         throw new \Exception('Unreacheable statement');
     }
     return $row;
 }
Exemple #3
0
 public function allWithIds($orderBy = null, $limit = null, $offset = null)
 {
     list($whereSql, $args) = $this->getWhereSqlAndArgs();
     return $this->table->allWithIds($whereSql, $args, $orderBy, $limit, $offset);
 }