Yii rest api在数据库中插入数据

时间:2014-05-31 11:46:11

标签: php mysql rest yii

我在休息服务方面遇到了一些问题。 我是yii框架中的新程序员,之前我没有创建过任何服务。 我的代码是下一个:

我的控制器:

    public function actionList()
     {
         $model=new Entryfile;
            if($_POST) {
                 $init = CJSON::decode(file_get_contents('php://input'));
                 $model->attributes=$init['input'];
                 $model->eeType=($model->eeType);
                 $model->eeSize=($model->eeSize);
                 $model->eeFileName=($model->eeFileName);
                 $model->eeFileChecksum=($model->eeFileChecksum);
                 $model->eeEntryPath=($model->eeEntryPath);
                 }
         $model=new Outputfile;
            if($_POST) {
                 $init = CJSON::decode(file_get_contents('php://input'));
                 $model->attributes=$init['output'];
                 $model->ofName=($model->ofName);
                 $model->ofDocumentPath=($model->ofDocumentPath);
                 $model->ofDocumentType=($model->ofDocumentType);

                 }

     }

Entryfile是一个表,输出文件是另一个表... 用于输入和输出。 在邮递员中我发了:

{ 
  "input": 
 { 
   "eeType": "doc",
   "eeSize": "1024", 
   "eeFileName": "something", 
   "eeFileChecksum": "23dsdjm34mn4", 
   "eeEntryPath": 1 
 },

  "output": 
  { 
    "ofDocumentType": "PDF", 
    "ofName": "something", 
    "ofDocumentPath": 1 
  } 
}

现在我只需要将邮件中的数据插入数据库。

3 个答案:

答案 0 :(得分:1)

使用save()方法。

$model=new Entryfile;
  ...
$init = CJSON::decode(file_get_contents('php://input'));
$model->attributes=$init['input'];
   ...
if (!$model->save())
{
   print_r($model->getErrors());
}

答案 1 :(得分:0)

你的意思是?

$model=new Outputfile;
        if($_POST) {
             $init = CJSON::decode(file_get_contents('php://input'));
             $model->attributes=$init['output'];
             if($model->save());
                {
                   $this->$model->ofName=($model->ofName);
                   $this->$model->ofDocumentPath=($model->ofDocumentPath);
                   $this->$model->ofDocumentType=($model->ofDocumentType); 
                }
            // echo "\n".'Output podaci:'.print_r($init['output'],1);

             }

答案 2 :(得分:0)

请尝试$ model-> save(false)。如果数据保存到数据库,而不仅仅是为模型设置安全属性。