/** * @param Model $model * @return Model|null * @throws Exception */ public function replace(Model $model) { $rtn = null; $data = $model->toArray(); $modified = $model->getModified(); $cols = []; $values = []; $params = []; foreach ($modified as $key) { $cols[] = $key; $values[] = ':' . $key; $params[':' . $key] = $data[$key]; } if (count($cols)) { $cols = '`' . implode('`, `', $cols) . '`'; $vals = implode(', ', $values); $query = "REPLACE INTO `{$this->table}` ({$cols}) VALUES ({$vals});"; $stmt = $this->connection->prepare($query); if ($stmt->execute($params)) { $modelId = !empty($data[$this->key]) ? $data[$this->key] : $this->connection->lastInsertId(); $this->cacheSet($data[$this->key], null); $rtn = $this->getByPrimaryKey($modelId, 'write'); $this->cacheSet($modelId, $rtn); } } return $rtn; }