/** * @param SavableModelInterface $model * @throws CouldNotPersistException */ public function persist(SavableModelInterface $model) { $params = ['index' => $this->index, 'type' => $this->type]; if ($model->getId()) { $params['body'] = ['doc' => $model->toArray()]; $params['id'] = $model->getId(); $this->client->update($params); $model->markAsStored(); return; } $params['body'] = $model->toArray(); $response = $this->client->index($params); $model->id = $response['_id']; $model->markAsStored(); }
/** * @param SavableModelInterface $model * @return string */ public function update(SavableModelInterface $model) { $bindData = array(); $index = 0; $update = ''; $data = $model->toArray(); $fields = $model->getFields(); foreach ($fields as $field) { $bindKey = 'data' . $index++; $bindData[$bindKey] = $data[$field]; $update .= $field . ' = :' . $bindKey . ', '; } $update = substr($update, 0, -2); $bindData['whereId'] = $model->getId(); $query = $this->pdo->prepare('update ' . $this->table . ' set ' . $update . ' where id = :whereId '); $query->execute($bindData); $query->closeCursor(); $model->markAsStored(); }
/** * @param SavableModelInterface $model * @return void */ public function persist(SavableModelInterface $model) { $this->collection->insertOne($model->toArray()); $model->markAsStored(); }