码迷,mamicode.com
首页 > 数据库 > 详细

Yii数据库操作方式之DAO

时间:2014-10-19 18:34:11      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   sp   数据   div   

1.DAO方式之数据插入

 1 public function actionInsert() {
 2                 $re = Yii::app()->db->createCommand()->insert(‘user‘,array(
 3                     ‘username‘=>‘赵六‘,
 4                     ‘city_id‘=>‘5‘,
 5                     ‘create_time‘=>time(),
 6                     ‘update_time‘=>time(),
 7                 ));
 8             p($re);
 9             $lastId = Yii::app()->db->getLastInsertID();
10             p($lastId);
11         }

2.DAO方式之数据更新

 public function actionUpdate($id) {
            $re = Yii::app()->db->createCommand()->update(‘user‘,array(
                ‘username‘=>‘张三一‘,
            ),‘id=:id‘,array(‘:id‘=>$id));
            p($re);
        }

3.DAO方式之数据删除

//DAO方式之删除
        public function actionDelete($id) {
            $re = Yii::app()->db->createCommand()->delete(‘user‘,‘id=:id‘,array(‘:id‘=>$id));
            p($re);
        }

4.DAO方式之数据查询

DAO方式的数据查询可分为四种  1.查询单条数据   2.查询多条数据  3.查询某个字段的值  4.查询某个字段的所有值

4-1.查询单条数据

public function actionRow($id) {
            $re = Yii::app()->db->createCommand()->select(‘id,username,city_id,create_time‘)->from(‘user‘)->where(‘id=:id‘,array(‘:id‘=>$id))->queryRow();
            p($re);
        }

4-2.查询多条数据

public function actionAll($id) {
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(‘id>:id‘,array(‘:id‘=>$id))->order(‘id desc‘)->queryAll();
        
            p($re);
        }

4-3.查询某个字段的值

public function actionScalar($id) {
            $re = Yii::app()->db->createCommand()->select(‘username‘)->from(‘user‘)->where(‘id=:id‘,array(‘:id‘=>$id))->queryScalar();
        
            p($re);
        }

4-4.查询某个字段的所有值

public function actionColumn($id) {
            $re = Yii::app()->db->createCommand()->select(‘username‘)->from(‘user‘)->where(‘id>:id‘,array(‘:id‘=>$id))->queryColumn();
        
            p($re);
        }

5.DAO方式之where条件的应用

public function actionWhere() {
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(‘id>:id and id<10‘,array(‘:id‘=>2))->order(‘id desc‘)->queryAll();
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(‘id>:id‘,array(‘:id‘=>2))->andWhere(‘id<:eid‘,array(‘:eid‘=>10))->order(‘id desc‘)->queryAll();
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(array(‘and‘,‘id>5‘,‘id<10‘))->order(‘id desc‘)->queryAll();
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(array(‘in‘,‘id‘,array(‘1‘,‘4‘,‘6‘,‘10‘)))->order(‘id desc‘)->queryAll();
            $re = Yii::app()->db->createCommand()->select(‘*‘)->from(‘user‘)->where(array(‘like‘,‘username‘,‘%六%‘))->order(‘id desc‘)->queryAll();
        
            p($re);
        }

6.DAO方式之关联查询join的应用

public function actionJoin() {
            $re = Yii::app()->db->createCommand()->select(‘u.username,c.name,u.create_time‘)->from(‘user u‘)->join(‘city c‘,‘u.city_id=c.id‘)->where(‘u.id>5‘)->queryAll();
        
            p($re);
        }

 

Yii数据库操作方式之DAO

标签:des   style   blog   color   io   ar   sp   数据   div   

原文地址:http://www.cnblogs.com/liuwz/p/4035137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!