親們, 我是yii小白 不要笑話我奧。今天白天寫一個管理模塊涉及到 yii ar 下的 curd 操作,做 update 操作時糾結了好久,今天晚上花點時間學習, 下面寫下我的測試記錄
代碼如下:
1 public function actionIndex(){
2
3 // 寫入數據
4 //yii ar curd 之 insert
5 $_POST['Users']['sex'] = 2;
6 $_POST['Users']['username'] = 'xiaohua';
7 $model = new Users;
8 $model->attributes = $_POST['Users'];
9 $rt = $model->save();
10
11 if($rt) {
12 Yii::app()->user->setFlash('success','寫入成功了');
13 $this->redirect(array('index/test'));
14 } else {
15 Yii::app()->user->setFlash('error','失敗了');
16 $this->redirect(array('index/test'));
17 }
18
19
20 // 查詢數據
21 //get one data by field
22 $hh = Users::model()->find(
23 'username=:name',array(':name'=>'fzb')
24 );
25 print_r($hh['username']);
26
27 //get one data by id
28 $hh = Users::model()->findByPk(1);
29 print_r($hh['username']);
30
31 //get all
32 $hh = Users::model()->findAll();
33 print_r($hh);
34
35 // 修改數據
36 // update one
37 $model = new Users;
38 $count = $model->updateByPk(5,array('username'=>'admin','sex'=>1));
39 if($count) {
40 Yii::app()->user->setFlash('success','修改成功了');
41 $this->redirect(array('index/test'));
42 } else {
43 Yii::app()->user->setFlash('error','修改失敗了');
44 $this->redirect(array('index/test'));
45 }
46
47
48 }
希望大嬸們嘴下留情
我還是小白呢
有不足之處希望大家指出,謝謝!