入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

YII进行数据查询及类库追踪

创建时间:2014-10-18 投稿人: 浏览次数:1222

一般处理过程:


模型进行数据操作,继承自CActiveRecord (活跃记录)

AR数据库向上的封装,AR通过OOP面向对象方式操作数据库。AR需要最终转变为具体的sql语句,通过一个中间类(criteria标准)协助转为的具体sql语句。find、findAll 就是转化为这个类的一些属性

文件路径AR  : frameworkdbarCActiveRecord.php

可以看到很多方法


打开findAll函数进行类库追踪到  这个frameworkdbschemaCDbCommandBuilder.php 文件

再次进行追踪到这个文件frameworkdbschemaCDbCriteria.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();
    }


所有控制器都继承CController,

l 父类Controller在哪了?



这个父类我们没有显示包含进程序里边,比如include、requre之类包含指令

l 那么这个父类控制器Controller我们在什么地方给包含进来的?

答:在Yii应用的主配置文件里边main.php,间接通过引入compoments组件目录进来的


l 那么主配置文件main.php在什么地方引入到我们的应用里边的呢?

答:在统一入口处index.php





声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。