mysql - Yii RESTFul with JSON format Update and Create -


i trying implement restful in yii framework json formatting. have single user table shown below:

create table user(     id int(10) not null,     email varchar(100) not null,     password varchar(64) not null,     nickname varchar(30) not null,     primary key(id) )engine=innodb; 

and in yii usercontroller.php class (my yii application has crud implemented gii).
have following 2 functions handle crud apis using restful , json formatting shown below:

public function actionread($id=''){     $requesttype = yii::app()->request->getrequesttype();     if($requesttype !== 'get' && $id === ''){         throw new exception('$id must specified or acces service get!');         return;     }      if($id !== '0'){         $user = yii::app()->db->createcommand('select * user id=:id limit 1');         $user->bindparam(':id', $id);         $user = $user->queryrow();     }else{         $user = yii::app()->db->createcommand('select * user 1');         $user = $user->queryall();     }      echo json_encode($user);  }   public function actionsave($id){     $requesttype = yii::app()->request->getrequesttype();     if($requesttype !== 'post'){         throw new exception('acces service post!');         return;     }      $post = yii::app()->request->getpost('form');      if($id !== ''){//update...need logic here update      }else{//create...need logic here create      }      $command = yii::app()->db->createcommand('insert user (id, email) values (null, :email)');     $command->bindparam(':email', $post['email']);       return json_encode($command->execute()); } 

with code above, able view , list data using client in google chrome. having trouble getting update , create working. have put place holders 2 implementations of "create" , "update" in code above in comments.

can ideas or solutions on how can achieve this.

if have user model, code like:

$post = yii::app()->request->getpost('form');  if($id !== ''){//update...need logic here update      $model = user::model()->findbypk($id); }else{//create...need logic here create      $model = new user; } $model->email = $post['email']; $model->nickname = $post['nickname']; $model->password = $post['password']; $model->save();  return json_encode($model->id); 

if $post has array of attributes.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -