目錄
一、增刪查改
二、驗(yàn)證規(guī)則
三、事務(wù)管理
四、名字空間。參考:Yii數(shù)據(jù)庫操作——名字空間(named scopes)的三種用法
一、增刪查改
1,創(chuàng)建
$post = new Post;
$post->title = "";
$post->content = "";
$post->created_at = "CDbExpression('NOW()')";
$post->save();
(1) 插入后可立即獲得主鍵id。
$id = $post->id; // 前提是auto_increment
(2) 某一個(gè)字段的值為缺省值時(shí),可以在models/Class.php中修改
Class Post extends CActiveRecord{
public $title = 'new title';
$post = new Post;
echo $post->title; // 輸出是: new title
}
(3) 使用CDbExpression
$post->create_time = new CDbExpression('NOW()');
2,查詢【待補(bǔ)充】
(1) 通過主鍵查詢
find("postID=:postID", array(':postID' => postID)
findByPk($id) // 單主鍵
(2) 通過非主鍵查詢
find("postID=:postID", array(':postID' => postID)
findAll( id = $id )
findAll( id IN ( $id ) )
3,更新【待補(bǔ)充】
先find,并將對(duì)應(yīng)字段賦新值,再保存
可以通過CActiveRecord::isNewRecord來判斷是新建,還是更新。
4,刪除
(1) 如果是一條記錄
先找到后刪除
$post=Post::model->findByPk(10);
$post->delete();
直接通過主鍵刪除(類級(jí)別刪除,不需要先載入記錄)
Post::model->deleteByPk(10);
(2) 如果是多條記錄(類級(jí)別刪除,不需要先載入記錄)
Post::model->deleteAll();
二、驗(yàn)證規(guī)則
驗(yàn)證規(guī)則(Data validation)發(fā)生在調(diào)用save()方法的時(shí)候。驗(yàn)證是基于在rules()方法中的定義。
if( $post->save() ){
// 驗(yàn)證通過
} else {
// 驗(yàn)證失敗。通過getErrors()返回錯(cuò)誤信息。
}
獲取用戶從表單提交的數(shù)據(jù)
$post->title = $_POST['title'];
$post->content = $_POST['content'];
$post->save();
如果多了,可以通過下面的方式減輕(alleviate)復(fù)雜程度:
- $post->attributes = $_POST['Post'];
- $post->save();
- //類似于:
- foreach($_POST['Post'] as $name=>$value){
- if($name is a safe attribute)
- $model->$name = $value;
- }
$post->attributes = $_POST['Post'];$post->save();//類似于:foreach($_POST['Post'] as $name=>$value){ if($name is a safe attribute) $model->$name = $value;}
注意:里面的驗(yàn)證檢驗(yàn)非常重要,否則用戶可能繞過授權(quán)。
三、事務(wù)管理
dbConnection是CDbConnection的實(shí)例
官方文檔
- $model = Post::model();
- $transaction = $model->dbConnection->beginTransaction();
- try{
- $post = $model->findByPk(10);
- $post->title = 'new post title';
- $post->save();
- $transaction->commit();
- } catch (Exception $e){
- $transaction->rollback();
- }
$model = Post::model();$transaction = $model->dbConnection->beginTransaction();try{ $post = $model->findByPk(10); $post->title = 'new post title'; $post->save(); $transaction->commit();} catch (Exception $e){ $transaction->rollback();}
實(shí)際項(xiàng)目
- $trans = Yii::app()->db->beginTransaction();
- try {
- $manufacturer = new Manufacturer();
- $manufacturer->name = $name;
- $manufacturer->email = $email;
- $manufacturer->save();
- $trans->commit();
- } catch (Exception $e) {
- $trans->rollback();
- $this->response(array('status' => 1, 'msg' => $e->getMessage()));
- }
$trans = Yii::app()->db->beginTransaction();try { $manufacturer = new Manufacturer(); $manufacturer->name = $name; $manufacturer->email = $email; $manufacturer->save(); $trans->commit();} catch (Exception $e) { $trans->rollback(); $this->response(array('status' => 1, 'msg' => $e->getMessage())); }
其實(shí)使用的時(shí)候跟凡客體的我是凡客或淘寶體的親一樣。
注:Yii::app()后面的db在../config/main.php中已配置
- 'components'=>array(
- 'user'=>array('allowAutoLogin'=>true,),
- 'db'=>array("數(shù)據(jù)庫連接參數(shù)"),
- )