Yii框架操作方法

1.yii数据查询

a.使用CDbCriteria数据查询

$attributes = array();

$criteria = new CDbCriteria;

//$criteria->select='amount';  

$criteria->condition='user_id=:user_id';

$criteria->params=array(':user_id'=>18889195);

$DwCouponModel = new Coupon();

$classResult = $DwCouponModel->findAllByAttributes($attributes,$criteria);

//返回一个数组,每个数组元素为一个对象

if(count($classResult)>0){

foreach($classResult as $key => $row) {

$returnResult[$key] = $row->getAttributes();

}

}

//将数组元素转换成数组

b.使用createCommand数据查询

$user = Yii::app()->db->createCommand()

        ->select('user_id,sn')

        ->from('dw_coupon')

        ->where('user_id=:user_id', array(':user_id'=>$this->user_id))

        ->queryAll();

        queryAll、execute、queryRow、queryColumn

2.目录结构

model:/protected/lib/models/Coupon.php(优惠券对应dw_coupon表)

view:/views/newuser/main/XXX.tpl

controller:/protected/modules/newuser/controllers/MainController.php

service:/protected/lib/services/

class:/protected/lib/classes/

3.主从数据库的切换

Yii::app()->db->switchToMaster();

        Yii::app()->db->switchToSlave();

4.yii拓页面

controller:

$this->user_id = xxx;

$returnResult = AccountService::getInstance()->getAccountInfo($this->user_id);

$this->render("test",$returnResult);

view:

<{$key.key.key}>

5.方法不存在是执行__call方法

执行到/itzlib/yiiext/ItzBaseService.php->getDataFromCache()通过第二个参数$method识别getSystem、getBorrow等


你可能感兴趣的:(yii)