Exemplo n.º 1
0
 function post_save($data)
 {
     $sql->null;
     if ($data->id) {
         //update
         $sql = Test::find($data->id);
         //date("d-m-Y H:i:s") date("Y-m-d  H:i:s") tanto faz
         $sql->update_attributes(array('nome' => $data->nome, 'user_id' => $data->user_id, 'updated' => date("Y-m-d  H:i:s")));
     } else {
         //insert
         $attributes = array('nome' => $data->nome, 'user_id' => $data->user_id, 'created' => date("Y-m-d  H:i:s"));
         $sql = new Test($attributes);
     }
     $sql->save();
     $retorno = $sql->to_json();
     return '{"result": [' . $retorno . ']}';
 }
Exemplo n.º 2
0
 public function edit($id = 0)
 {
     $obj = new Test();
     $obj->get_by_id((int) $id);
     if (!$_POST) {
         echo $obj->to_json();
     } else {
         if (isset($_POST['model']) and $model = $_POST['model']) {
             $obj->from_json($model);
             if ($obj->save()) {
                 $this->_user->save($obj);
                 //保存关系
                 echo $obj->to_json();
             } else {
                 echo json_encode(array('error' => $obj->error->string));
             }
         } else {
             if (isset($_POST['_method']) and $_POST['_method'] === 'DELETE') {
                 $this->_user->delete($obj);
                 $obj->delete();
             }
         }
     }
 }