模型进行数据操作,继承自CActiveRecord (活跃记录)
AR数据库向上的封装,AR通过OOP面向对象方式操作数据库。AR需要最终转变为具体的sql语句,通过一个中间类(criteria标准)协助转为的具体sql语句。find、findAll 就是转化为这个类的一些属性
文件路径AR : \framework\db\ar\CActiveRecord.php
可以看到很多方法
打开findAll函数进行类库追踪到 这个\framework\db\schema\CDbCommandBuilder.php 文件
再次进行追踪到这个文件\framework\db\schema\CDbCriteria.php:
为什么这样追踪:
1
2
3
常用查询方法
下面是三种查询方式
function actionCeshi(){
$model = Goods::model();
//$infos = $model -> findAllByPk(10);
//$infos = $model -> findAllByPk(array(1,5,12));
//////////////////////////////////////////////////////////////////////////////////////////
//findAll($condition,$param)
//$condition 就是sql语句的where条件
//查询诺基亚手机并且价格大于500元
//$infos = $model -> findAll("goods_name like '诺%' and goods_price>500");
//为了避免sql注入的安全问题,sql语句里边最好不要直接写条件信息
//$infos = $model -> findAll("goods_name like :name and goods_price>:price",array(':name'=>'诺%',':price'=>500));
//////////////////////////////////////////////////////////////////////////////////////////
//有的时候我们查询信息,
//想要查询具体的"字段" select
//想要查询具体的"条件" condition
//想要查询具体的"排序" order
//想要查询具体的"分组" group
//想要查询具体的"限制" limit
//想要查询具体的"偏移量" offset
//$infos = $model -> findAll(array(
// 'select'=>'goods_name,goods_price',
// 'condition'=>"goods_name like '诺%'",
// 'order'=>'goods_price desc',
// 'limit'=>3,
// 'offset'=>6,
//));
//////////////////////////////////////////////////////////////////////////////////////////
//通过criteria实现信息的查询
$criteria = new CDbCriteria();
$criteria -> select = "goods_name,goods_price";
$criteria -> condition = "goods_name like '摩%'";
//$criteria -> limit = 6;
$criteria -> order = "goods_price";
$infos = $model -> findAll($criteria);
$this ->renderPartial('show',array('goods_infos'=>$infos));
//save()方法执行update或insert
//$model -> save();
}
原文:http://blog.csdn.net/buyingfei8888/article/details/40227659