AR模型是ORM方式的一种,其将SQL查询进行封装,使得数据库读写更加方便便捷。其中一个AR类代表数据库中的一张表。
1.类的定义(Model模型)
定义方式如下:
class Post extends CActiveRecord {
publicstaticfunction model($className=__CLASS__) {
return parent::model($className);
}
publicfunction tableName() {
return 'tbl_post';
}
}
这是一个Model类的最小代码。
2.新增记录
$post=new Post;
$post->title='sample post';
$post->content='content for the sample post';
$post->create_time=time();
$post->save();
3.查询
查询语句比较复杂,主要有四种。
// find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attribute values
$post=Post::model()->findByAttributes($attributes,$condition,$params);
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);
其中$condition部分就是SQL语句的WHERE部分,$params就是$condition里面需要的参数。示例:
// find the row with postID=10
$post=Post::model()->find('postID=:postID', array(':postID'=>10));
同时也可以使用CDbCriteria类来配置查询条件。示例如下:
$criteria=new CDbCriteria;
$criteria->select='title'; // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria); // $params is not needed
此时就无需$params参数了。
上述是查询单条记录,如果要查询多条记录则使用:
// find all rows satisfying the specified condition
$posts=Post::model()->findAll($condition,$params);
// find all rows with the specified primary keys
$posts=Post::model()->findAllByPk($postIDs,$condition,$params);
// find all rows with the specified attribute values
$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
// find all rows using the specified SQL statement
$posts=Post::model()->findAllBySql($sql,$params);
4.更新记录
更新记录也可以用save()函数。AR模型会智能判定,若对象由new创建则新建记录,若对象是从数据库中的查询结果,则save函数就变成了更新。
然而更新也有特定的函数:
// update the rows matching the specified condition
Post::model()->updateAll($attributes,$condition,$params);
// update the rows matching the specified condition and primary key(s)
Post::model()->updateByPk($pk,$attributes,$condition,$params);
// update counter columns in the rows satisfying the specified conditions
Post::model()->updateCounters($counters,$condition,$params);
5.删除记录
先查询后删除:
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10
$post->delete(); // delete the row from the database table
直接删除:
// delete the rows matching the specified condition
Post::model()->deleteAll($condition,$params);
// delete the rows matching the specified condition and primary key(s)
Post::model()->deleteByPk($pk,$condition,$params);
6.从表单直接提交到数据库
如果数据表的项太多,插入新记录的时候参数列表可能会很长,这时候可以使用attributes属性来承接由表单POST过来的参数。(未测试)
// assume $_POST['Post'] is an array of column values indexed by column names
$post->attributes=$_POST['Post'];
$post->save();
7.函数重载
在执行记录的时候可以伴随着函数调用,这些函数主要有:beforeValidate, afterValidate, beforeSave, afterSave, beforeDelete, afterDelete, afterConstruct, beforeFind, afterFind, 函数名可以看出是什么意思了。
8. 使用事务
事务可以将读写操作作为一个整体进行,有效防止数据库读写出错。
$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try {
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
} catch(Exception$e) {
$transaction->rollback();
1.类的定义(Model模型)
定义方式如下:
class Post extends CActiveRecord {
publicstaticfunction model($className=__CLASS__) {
return parent::model($className);
}
publicfunction tableName() {
return 'tbl_post';
}
}
这是一个Model类的最小代码。
2.新增记录
$post=new Post;
$post->title='sample post';
$post->content='content for the sample post';
$post->create_time=time();
$post->save();
3.查询
查询语句比较复杂,主要有四种。
// find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attribute values
$post=Post::model()->findByAttributes($attributes,$condition,$params);
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);
其中$condition部分就是SQL语句的WHERE部分,$params就是$condition里面需要的参数。示例:
// find the row with postID=10
$post=Post::model()->find('postID=:postID', array(':postID'=>10));
同时也可以使用CDbCriteria类来配置查询条件。示例如下:
$criteria=new CDbCriteria;
$criteria->select='title'; // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria); // $params is not needed
此时就无需$params参数了。
上述是查询单条记录,如果要查询多条记录则使用:
// find all rows satisfying the specified condition
$posts=Post::model()->findAll($condition,$params);
// find all rows with the specified primary keys
$posts=Post::model()->findAllByPk($postIDs,$condition,$params);
// find all rows with the specified attribute values
$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
// find all rows using the specified SQL statement
$posts=Post::model()->findAllBySql($sql,$params);
4.更新记录
更新记录也可以用save()函数。AR模型会智能判定,若对象由new创建则新建记录,若对象是从数据库中的查询结果,则save函数就变成了更新。
然而更新也有特定的函数:
// update the rows matching the specified condition
Post::model()->updateAll($attributes,$condition,$params);
// update the rows matching the specified condition and primary key(s)
Post::model()->updateByPk($pk,$attributes,$condition,$params);
// update counter columns in the rows satisfying the specified conditions
Post::model()->updateCounters($counters,$condition,$params);
5.删除记录
先查询后删除:
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10
$post->delete(); // delete the row from the database table
直接删除:
// delete the rows matching the specified condition
Post::model()->deleteAll($condition,$params);
// delete the rows matching the specified condition and primary key(s)
Post::model()->deleteByPk($pk,$condition,$params);
6.从表单直接提交到数据库
如果数据表的项太多,插入新记录的时候参数列表可能会很长,这时候可以使用attributes属性来承接由表单POST过来的参数。(未测试)
// assume $_POST['Post'] is an array of column values indexed by column names
$post->attributes=$_POST['Post'];
$post->save();
7.函数重载
在执行记录的时候可以伴随着函数调用,这些函数主要有:beforeValidate, afterValidate, beforeSave, afterSave, beforeDelete, afterDelete, afterConstruct, beforeFind, afterFind, 函数名可以看出是什么意思了。
8. 使用事务
事务可以将读写操作作为一个整体进行,有效防止数据库读写出错。
$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try {
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
} catch(Exception$e) {
$transaction->rollback();
}
参考:http://www.cnblogs.com/duanxz/p/3465727.html