美文网首页
yii 框架Dao 数据操作用法

yii 框架Dao 数据操作用法

作者: 孤岛渔夫 | 来源:发表于2017-06-02 18:11 被阅读0次
<?php

/**
 * Created by PhpStorm.
 * Date: 2017/6/2
 * Time: 17:08
 */
class Dao extends Controller
{
    //注意:涉及到表名要用{{表名}}才能自动加上表前缀
    //执行原生语句
    public function actionSql()
    {
        $res = Yii::app()->db->createCommand($sql)->execute();//返回影响条数
        //使用createCommand()操作数据库对象时,数据库返回的结果是数组
        createCommand($sql)->queryRow();//$sql 原生 sql语句//返回单条
        CreateCommand($sql)->queryAll();//返回集合
    }

    //插入
    public function actionCreate()
    {
        $res = Yii::app()->db->createCommand()->insert('表名', ['字段名' => '值']);
        Yii::app()->db->getLastInsertID();//获取最新插入的ID
    }

    //修改
    public function actionUpdate()
    {
        $res = Yii::app()->db->createCommand()->update('表名', ['字段名' => '值'], '字段名 > :值', [':值' => '值']);
    }

    //删除
    public function actionDelete()
    {
        $res = Yii::app()->db->createCommand()->delete('表名', '字段名 = :值', [':值' => '值']);
    }

    //查单条
    public function actionRow()
    {
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where('字段名 = :值', [':值' => '值'])->queryRow();
    }

    //查多条
    public function actionAll()
    {
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where('字段名 > :值', [':值' => '值'])->queryAll();
    }

    //统计数据 数量
    public function actionScalar()
    {
        $res = Yii::app()->db->createCommand()->select('count(字段名)')->from('表名')->where('字段名 < :值', [':值' => '值'])->queryScalar();
    }

    //指定列查询 id
    public function actionColumn()
    {
        $res = Yii::app()->db->createCommand()->select('id')->from('表名')->where('字段名 > :值', [':值' => '值'])->queryColumn();
    }

    //复杂查询方式
    public function actionAnd()
    {
        //查询ID 大于 1 并且 小于 5 的所有数据集合
        //and() 字串形式
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where('字段名1 > :值1 and 字段名1 < :值2', [':值1' => '值1', ':值2' => '值2'])->queryAll();

        //and() 数组形式
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where(['and', '字段名1 > 值1', '字段名1 < 值2'])->queryAll();

        //andWhere() 方法
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where('字段名 > :值', [':值' => '值'])->andWhere('字段名 < :值', [':值' => '值'])->queryAll();
    }

    //使用in 查询出 ID 在 5 和 6 之间的数据集合
    public function actionIn()
    {
        $res = Yii::app()->db->creatCommand()->select('字段名,字段名')->from('表名')->where('in', '字段名ID', [5, 6])->queryAll();
    }

    //使用like 查询 并且 id 大于6 小于 10
    public function actionLike()
    {
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where(['like', '字段名', '%关键字%'])->andWhere(['and', 'id > 6', 'id < 10'])->queryAll();
    }

    //查询出名字里 带有关键字的 的三条数据 ID 倒序 略过3条数据
    public function actionLimit()
    {
        $res = Yii::app()->db->createCommand()->select('字段名,字段名')->from('表名')->where(['like', 'name', '%关键字%'])->limit(3)->offset(3)->order('id desc')->queryAll();
    }

    //关联查询
    public function actionJoin()
    {
        //查询 ID 大于2 小于6 的所有用户信息 并查询 城市名称
        $res = Yii::app()->db->createCommand()->select('u.id,u.name,c.name')
            ->from('表1 u')
            ->join('表2 c', 'c.id = u.cid')
            ->where('u.id > 2')
            ->andWhere('u.id < 6')
            ->queryAll();
    }


}

相关文章

  • yii 框架Dao 数据操作用法

  • yii 框架 model 数据操作用法

  • Yii快速入门(三)数据库操作

    Yii提供了强大的数据库编程支持。Yii数据访问对象(DAO)建立在PHP的数据对象(PDO)extension上...

  • 探索SSM(一)-MyBatis

    SSM框架在我看来可总结为Spring:MVC代替ServletMybatis:Dao中数据库操作,简化数据库操作...

  • Hibernate(01)

    首先认识Hibernate框架是ORM关系映射框架, 工作在持久(dao)层,用对象的方式操作sql数据库 优点:...

  • MyBatis框架技术

    MyBatis框架代替原来DAO层的工作,框架为我们实现数据库操作,并进行了简化,方便使用 MyBatis框架的工...

  • MyBatis学习总结

    一、认识mybatis mybatis是一个持久层框架(dao - 数据库操作)?Github源码?mybatis...

  • 【Yii 多数据库连接】配置

    在yii项目中,连接多数据库进行操作。step1:配置main.php 文件。 框架默认是 加载db数据库的,如果...

  • Mybatis

    Mybatis是SSM框架中的M。操作持久层,代替dao,hibernate。提供半自动数据查询。 一.MyBat...

  • Mybatis

    Mybatis是SSM框架中的M。操作持久层,代替dao,hibernate。提供半自动数据查询。 一.MyBat...

网友评论

      本文标题:yii 框架Dao 数据操作用法

      本文链接:https://www.haomeiwen.com/subject/cbbufxtx.html