/** * @param array $relationData * @param \Phalcon\Mvc\Model $model * @param string $relationName * @return mixed * @throws Exception */ private function update(array $relationData, $model, $relationName) { if (empty($relationData['id'])) { throw new Exception('relation "' . $relationName . '" have need id for update operation.'); } $id = intval($relationData['id']); $relation = $model->getModelsManager()->getRelationByAlias(get_class($model), $relationName); $relationClass = $relation->getReferencedModel(); $modelRelation = $relationClass::findFirstById($id); if (!$modelRelation) { throw new Exception($relationClass . '::findFirstById, with id=' . $id . ' not found'); } $modelRelation->assign($relationData); $this->securityManager->check(array('model' => $modelRelation, 'action' => CrudOperations::CREATE, 'modelName' => get_class($modelRelation), 'params' => $relationData)); $this->logManager->setOldModel($modelRelation); $result = $modelRelation->save(); if ($result) { $this->logManager->updateModel($modelRelation); } return $result; }
/** * Delete record from DB. * @return array */ private function delete() { $params = Params::getMergeParams($this->request); if (empty($params['id'])) { return array('success' => false, 'msg' => 'param id is require'); } $id = intval($params['id']); $modelName = Analyzer::getModelName($this->request->get('_url')); /** * @var \Phalcon\Mvc\Model $model */ $model = $modelName::findFirst($id); if (!$model) { return array('success' => false, 'msg' => 'Record with id ' . $id . ' not found'); } $this->securityManager->check(array('model' => $model, 'action' => CrudOperations::DELETE, 'modelName' => $modelName, 'params' => $params)); $this->logManager->deleteModel($model); if ($model->delete()) { $this->logManager->deleteModel($model); return array('success' => true, 'msg' => 'Record with id ' . $id . ' has removed!'); } return array('success' => false, 'msg' => $this->jsonRecursiveGetMsg($model->getMessages())); }