YII 框架常用语法 常用数据库操作语法

YII 框架常用数据库操作语法

/********************数据库操作的相关语法**************/
/********************查询**************/
$collect = $model->find(array('condition'=>'status > 0 ','order'=>'id desc','limit'=>5));
$collect->addtime;

Yii::app()->db->createCommand($sql)->queryAll(); 
Yii::app()->db->createCommand($sql)->queryRow();

Yii::app()->db->createCommand($sql)->execute(); 


/********************增加**************/
$model = new Industry();
$model->setAttribute('name', "");    
$model->setAttribute('addtime', time());
if($model->save()){
$this->redirect('/seller/industry/index/');
Yii::app()->end();
}

/****************************统计*******************/

$num = $this->count(array("condition"=>"uid={$id} and token={$token}"));

//获取上次插入记录后的id
Yii::app()->db->getLastInsertID();

/********************修改**************/
//第一种方式
$user = new User;
$userinfo['updatetime'] = time();
$rs=$user->updateAll($userinfo, 'id=:user_id', array(':user_id'=>$user_id));
//第二种方式
public function actionUpdate($id){
$model = $this->loadModel($id);
$model->setAttribute('name', "");                     
$model->setAttribute('addtime', time());
if($model->save()){
$this->redirect('/seller/industry/index/');
Yii::app()->end();
}
}
public function loadModel($id)
{
$project=new Industry();
$model = $project->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}

//处理报错
print_r($model->getErrors());


//获取域名
Yii::app()->request->hostInfo

//获取参数 get或者post传值
Yii::app()->request->getParam('id');



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值