• 技术文章 >php教程 >php手册

    Yii基于数组和对象的Model查询技巧实例详解,yiimodel

    2016-06-13 08:48:52原创389

    Yii基于数组和对象的Model查询技巧实例详解,yiimodel


    本文实例讲述了Yii基于数组和对象的Model查询技巧。分享给大家供大家参考,具体如下:

    对于一个Model Post 有如下的4中查询方法,返回对象或者对象数组。

    //查找满足指定条件的结果中的第一行 find the first row satisfying the specified condition
    $post=Post::model()->find($condition,$params);
    //查找具有指定主键值的那一行 find the row with the specified primary key
    $post=Post::model()->findByPk($postID,$condition,$params);
    //查找具有指定属性值的行 find the row with the specified attribute values
    $post=Post::model()->findByAttributes($attributes,$condition,$params);//未找到返回null
    //通过指定的SQL 语句查找结果中的第一行 find the first row using the specified SQL statement
    $post=Post::model()->findBySql($sql,$params);
    
    

    如果find 方法找到了一个满足查询条件的行,它将返回一个Post 实例,实例的属性含有数据表行中相应列的值。然后我们就可以像读取普通对象的属性那样读取载入的值,例如echo $post->title;。如果使用给定的查询条件在数据库中没有找到任何东西, find 方法将返回null。

    调用find 时,我们使用$condition 和$params 指定查询条件。此处$condition 可以是SQL 语句中的WHERE 字符串,$params 则是一个参数数组,其中的值应绑定到$condation 中的占位符。例如:假设我们查询postID = 10的数据

    // find the row with postID=10
    $post=Post::model()->find('postID=:postID', array(':postID'=>10));
    
    

    条件$condition 就是我们sql里的where部分,那参数怎么办呢,通过params传递,不过名字是加了":"的。

    YII有个CDbCriteria类来构造查询,如果我们查询postId为10的title,CdbCriteria是这样构造的

    $criteria=new CDbCriteria;
    $criteria->select='title'; // only select the 'title' column
    $criteria->condition='postID=:postID';
    $criteria->params=array(':postID'=>10);
    $post=Post::model()->find($criteria); // $params is not needed
    
    

    一种替代CDbCriteria 的方法是给find 方法传递一个数组。数组的键和值各自对应标准( criterion)的属性名和值,上面的例子可以重写为如下:

    $post=Post::model()->find(array(
      'select'=>'title',
      'condition'=>'postID=:postID',
      'params'=>array(':postID'=>10),
    ));
    
    

    当然也适用于findAll()

    self::$_items[$type]=array();
    $models=self::model()->findAll(array(
      'condition'=>'type=:type',
      'params'=>array(':type'=>$type),
      'order'=>'position',
    ));
    
    

    当一个查询条件是关于按指定的值匹配几个列时,我们可以使用findByAttributes()。我们使$attributes 参数是一个以列名做索引的值的数组。
    findByAttributes 里的$attributes就是字段的名字.查询title为abc怎么查询呢?见下面
    复制代码 代码如下:Post::model()->findByAttributes(array('title'=>'abc'))

    其它方法:

    1、$admin=Admin::model()->findAll($condition,$params);

    该方法是根据一个条件查询一个集合,如:
    复制代码 代码如下:findAll("username=:name",array(":name"=>$username));
    2、$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
    findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));
    该方法是根据主键查询一个集合,可以使用多个主键,如:
    复制代码 代码如下:findAllByPk(array(1,2));
    3、$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);

    该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面,如:
    复制代码 代码如下:findAllByAttributes(array('username'=>'admin'));
    4、$admin=Admin::model()->findAllBySql($sql,$params);

    该方法是根据SQL语句查询一个数组,如:
    复制代码 代码如下:findAllBySql("select *from admin where username=:name",array(':name'=>'admin'));

    二、查询对像的方法

    1、$admin=Admin::model()->findByPk($postID,$condition,$params);

    根据主键查询出一个对象,如:复制代码 代码如下:findByPk(1);
    2、$row=Admin::model()->find($condition,$params);

    根据一个条件查询出一组数据,可能是多个,但是他只返回第一行数据,如:
    复制代码 代码如下:find('username=:name',array(':name'=>'admin'));
    3、$admin=Admin::model()->findByAttributes($attributes,$condition,$params);

    该方法是根据条件查询一组数据,可以是多个条件,把条件放到数组里面,他查询的也是第一条数据,如:
    复制代码 代码如下:findByAttributes(array('username'=>'admin'));
    4、$admin=Admin::model()->findBySql($sql,$params);

    该方法是根据SQL语句查询一组数据,他查询的也是第一条数据,如:
    复制代码 代码如下:findBySql("select *from admin where username=:name",array(':name'=>'admin'));
    5、拼一个获得SQL的方法,在根据find查询出一个对象

    $criteria=new CDbCriteria;
    $criteria->select='username'; // only select the 'title' column
    $criteria->condition='username=:username';
    $criteria->params=array(':username=>'admin');
    $post=Post::model()->find($criteria); // $params is not needed
    
    

    三、查询个数,判断查询是否有结果

    1、$n=Post::model()->count($condition,$params);

    该方法是根据一个条件查询一个集合有多少条记录,返回一个int型数字,如
    复制代码 代码如下:count("username=:name",array(":name"=>$username));
    2、$n=Post::model()->countBySql($sql,$params);

    该方法是根据SQL语句查询一个集合有多少条记录,返回一个int型数字,如
    复制代码 代码如下:countBySql("select *from admin where username=:name",array(':name'=>'admin'));
    3、$exists=Post::model()->exists($condition,$params);
    该方法是根据一个条件查询查询得到的数组有没有数据,如果有数据返回一个true,否则没有找到

    四、添加的方法

    $admin=new Admin;
    $admin->username=$username;
    $admin->password=$password;
    if($admin->save()>0){
      echo "添加成功";
    }else{
      echo "添加失败";
    }
    
    

    五、修改的方法

    1、Post::model()->updateAll($attributes,$condition,$params);

    $count = Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1'));
    if($count>0){
      echo "修改成功";
    }else{
      echo "修改失败";
    }
    
    

    2、Post::model()->updateByPk($pk,$attributes,$condition,$params);

    $count = Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin'));
    $count = Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin'));
    if($count>0){
      echo "修改成功";
    }else{
      echo "修改失败";
    }
    
    

    $pk代表主键,可以是一个也可以是一个集合,$attributes代表是要修改的字段的集合,$condition代表条件,$params传入的值

    3、Post::model()->updateCounters($counters,$condition,$params);

    $count =Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin'));
    if($count>0){
      echo "修改成功";
    }else{
      echo "修改失败";
    }
    
    

    array('status'=>1)代表数据库中的admin表根据条件username='admin',查询出的所有结果status字段都自加1

    六、删除的方法

    1、Post::model()->deleteAll($condition,$params);

    $count = Admin::model()->deleteAll('username=:name and password=:pass',array(':name'=>'admin',':pass'=>'admin'));
          $id=1,2,3
          deleteAll('id in(".$id.")');删除id为这些的数据
    if($count>0){
      echo "删除成功";
    }else{
      echo "删除失败";
    }
    
    

    2、Post::model()->deleteByPk($pk,$condition,$params);

    $count = Admin::model()->deleteByPk(1);
    $count = Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin'));
    if($count>0){
      echo "删除成功";
    }else{
      echo "删除失败";
    }
    
    

    希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

    您可能感兴趣的文章:

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Yii Model 查询 yii
    上一篇:php实现倒计时效果, 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • PHP弹出提示框并跳转到新页面即重定向到新页面• 利用PHP实现智能文件类型检测的实现代码• PHP 页面跳转到另一个页面的多种方法方法总结• 我的论坛源代码(五)• 建立文件交换功能的脚本(二)
    1/1

    PHP中文网