public function update(ModelInterface $model, $options = []) { $data = $model->getChanges(); $data[$this->getIdFieldName()] = $model->getId(); $this->addToRepo($data); $model->commitChanges(); return $model; }
public function save(ModelInterface $model, $options = []) { /** * Logic: * We assume that if _id is set it can only be an update. This is incorrect theoretically, but works in practice. * If I want to insert a document with a specific id, this logic would stop me. Luckily it's rare. * To get past this limitation, we can set an attribute in the options array, which gets stripped here. * This parameter is 'forceInsert' * * This logic is also implemented in \Dyln\DaoInterface\DaoInterface::save */ $forceInsert = false; if (isset($options['forceInsert'])) { $forceInsert = $options['forceInsert']; } $model->setProperty('upt', microtime(true)); if ($model->getId() && !$forceInsert) { $model = $this->getDao()->update($model, $options); } else { $model = $this->getDao()->save($model, $options); } return $model; }
/** * @param ModelInterface $model * @param array $options * @return ModelInterface */ public function update(ModelInterface $model, $options = []) { if (!isset($options['w'])) { $options['w'] = 1; } $model->setProperty('upt', microtime(true)); $data = $model->getChanges(); $condition = [$this->getIdFieldName() => $model->getProperty($this->getIdFieldName())]; $operation = ['$set' => $data]; $res = $this->getDbAdapter()->selectCollection($this->getTableName())->updateOne($condition, $operation, $options); $model->commitChanges(); return $model; }
public function update(ModelInterface $model, $options = []) { $model->setProperty('upt', microtime(true)); $data = $model->getChanges(); $condition = $this->getIdFieldName() . '=' . $model->getId(); $sql = 'UPDATE ' . $this->tableName . ' SET '; $values = []; $i = 1; foreach ($data['set'] as $key => $value) { $values[':param' . $i] = $value; $sql .= $key . '=' . ':param' . $i . ','; $i++; } foreach ($data['unset'] as $key => $value) { $values[':param' . $i] = null; $sql .= $key . '=' . ':param' . $i . ','; $i++; } $sql = rtrim($sql, ','); $sql .= ' WHERE ' . $condition; $stmt = $this->getDbAdapter()->prepare($sql); foreach ($values as $name => $value) { $stmt->bindValue($name, $value); } $stmt->execute(); $model->commitChanges(); return $model; }