コード例 #1
0
 /**
  * Save and create record to DB.
  * @return array
  */
 private function save()
 {
     $params = Params::getMergeParams($this->request);
     $id = intval(@$params['id']);
     $modelName = Analyzer::getModelName($this->request->get('_url'));
     $this->logManager->setRequestId();
     if ($id) {
         /**
          * @var \Phalcon\Mvc\Model $model
          */
         $model = $modelName::findFirst($id);
         if (!$model) {
             return array('success' => false, 'msg' => 'Record with id ' . $id . ' not found');
         }
         $model->assign($params);
         $this->securityManager->check(array('model' => $model, 'action' => CrudOperations::UPDATE, 'modelName' => $modelName, 'params' => $params));
         $this->logManager->setOldModel($model);
         if ($model->save()) {
             $this->logManager->updateModel($model);
             $errors = $this->saveRelations($model, Params::getRelations($this->request));
             if ($errors !== array()) {
                 return array('success' => false, 'msg' => implode('<br>', $errors));
             }
             return array('success' => true, 'msg' => 'Record with id ' . $id . ' saved!');
         }
         return array('success' => false, 'msg' => $this->jsonRecursiveGetMsg($model->getMessages()));
     }
     unset($params['id']);
     /**
      * @var \Phalcon\Mvc\Model $model
      */
     $model = new $modelName();
     $model->assign($params);
     $this->securityManager->check(array('model' => $model, 'action' => CrudOperations::CREATE, 'modelName' => $modelName, 'params' => $params));
     if ($model->save()) {
         $this->logManager->createModel($model);
         $errors = $this->saveRelations($model, Params::getRelations($this->request));
         if ($errors !== array()) {
             return array('success' => false, 'msg' => implode('<br>', $errors));
         }
         return array('success' => true, 'msg' => 'New record saved!', 'id' => $model->id);
     }
     return array('success' => false, 'msg' => $this->jsonRecursiveGetMsg($model->getMessages()));
 }