资讯专栏INFORMATION COLUMN

Yii 1.0数据库操作 查询、增加、更新、删除

malakashi / 3438人阅读

摘要:根据条件查询一个集合根据主键查询一个集合可以使用多个主键根据条件查询一个集合可以是多个条件把条件放到数组里面根据语句查询一个数组根据主键查询出一个对象根据条件查询出一组数据可能是多个但是他只返回第一行数据根据

1、根据条件查询一个集合

$objectResult=Post::model()->findAll($condition,$params);
$oClasses = classes::model()->findAll(array(
            "select"=>"classId,className,grade,type,status",
            "condition"=>"classId=21 and grade=1",
            "order"=>"createtime desc",
        ));
$objectResult=Post::model()->findAll("username=:name",array(":name"=>$username));
$objectResult=RepairItem::model()->findAll("orderno=:orderno and orderpostid=:orderpostid",array(":orderno"=>$orderInfo["orderno"],":orderpostid"=>$orderInfo["orderpostid"]));

$infoArr = NewsList::model()->findAll("status = "1" ORDER BY postid DESC limit 10 ");

2、根据主键查询一个集合,可以使用多个主键 findAllByPk

$objectResult=Post::model()->findAllByPk($postIDs,$condition,$params);
$objectResult=Post::model()->findAllByPk($postid,"name like :name and age=:age",array(":name"=>$name,"age"=>$age));
$objectResult=Post::model()->findAllByPk(array(1,2));

3、根据条件查询一个集合,可以是多个条件,把条件放到数组里面 findAllByAttributes

$objectResult=Post::model()->findAllByAttributes($attributes,$condition,$params);
$objectResult=Post::model()->findAllByAttributes(array("username"=>"jack"));

4、根据SQL语句查询一个数组 findAllBySql

$arrResult=Post::model()->findAllBySql($sql,$params);
$arrResult=Post::model()->findAllBySql("select * from tbl_post where username like :name",array(":name"=>"?%"));

5、根据主键查询出一个对象 eg:findByPk(1);

$arrResult=Post::model()->findByPk($postID,$condition,$params);
$arrResult=Post::model()->findByPk(1);

6、根据条件查询出一组数据,【可能是多个,但是他只返回第一行数据】

$arrRow=Post::model()->find($condition,$params);
$arrRow=Post::model()->find("username=:name",array(":name"=>"jack"));

7、根据条件查询一组数据,【可以是多个条件,把条件放到数组里面,查询的也是第一条数据】

$objectResult=Post::model()->findByAttributes($attributes,$condition,$params);
$objectResult=Post::model()->findByAttributes(array("username"=>"objectResult"));

8、根据SQL语句查询一组数据,【查询的也是第一条数据】

$objectResult=Post::model()->findBySql($sql,$params);
$objectResult=Post::model()->findBySql("select * from objectResult where username=:name",array(":name"=>"objectResult"));

9、通过CDbCriteria类find查询出一个对象

$criteria=new CDbCriteria; 
$criteria->select="username"; // 限制显示哪些字段 
$criteria->condition="username=:username";     //一个查询条件用aCondition.多条件用addCondition
$criteria->params=array(":username=>"jack"");
$criteria->order = "postsort DESC";
$criteria->limit = "3";
$post=Post::model()->find($criteria);

10、多条件查询的语句

$criteria = new CDbCriteria;     
$criteria->addCondition("postid=1"); //等同于 where postid = 1 
$criteria->addInCondition("postid", array(1,2,3,4,5)); //等同于 where postid IN (1,2,3,4,5,); 
$criteria->addNotInCondition("postid", array(1,2,3,4,5));//等同于 NOT IN (1,2,3,4,5,)
$criteria->addCondition("postid=1","OR");//等同于 OR而非AND 
$criteria->addSearchCondition("username", "jack");//等同于 where name like "%jack%" 
$criteria->addBetweenCondition("postid", 1, 4);// 等同于 between 1 and 4
$criteria->compare("postid", 1);    //根据你的参数自动处理成addCondition或者addInCondition.
$criteria->compare("postid", array(1,2,3));   //数组就会调用addInCondition 
 
 
$criteria->select = "postid,parentid,name"; //限制显示哪些字段 
$criteria->join = "xxx"; //连接表 
$criteria->with = "xxx"; //调用relations  
$criteria->limit = 10;    //取1条数据,如果小于0,则不作处理 
$criteria->offset = 1;   //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10 
$criteria->order = "xxx DESC,XXX ASC" ;//排序条件 
$criteria->group = "group 条件"; 
$criteria->having = "having 条件 "; 
$criteria->distinct = FALSE; //是否唯一查询

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

根据一个条件查询一个集合有多少条记录,返回一个int型数字

$intCount=Post::model()->count($condition,$params);
$intCount=Post::model()->count("username=:name",array(":name"=>$username));

根据SQL语句查询一个集合有多少条记录,返回一个int型数字

$intCount=Post::model()->countBySql($sql,$params);
$intCount=Post::model()->countBySql("select * from objectResult where username=:name",array(":name"=>"objectResult"));

根据一个条件查询查询得到的数组有没有数据,有数据返回一个true,否则没有找到

$boolExists=Post::model()->exists($condition,$params);
$boolExist=Post::model()->exists("name=:name",array(":name"=>$username));

四、添加的方法

$objectPost = new Post;       
$objectPost->username = $username;
$objectPost->password = $password;

或许

$objectPost->attributes = $arrNewData;

if($objectPost->save()){
    $intPostId= $objectPost->primaryKey; //生成主键id
    echo "添加成功";
}else{
    echo "添加失败";
}

五、修改的方法

Post::model()->updateAll($attributes,$condition,$params);
$count =Post::model()->updateAll(array("username"=>"11111","password"=>"11111"),"password=:pass",array(":pass"=>"1111a1"));
if($count > 0){
    echo "修改成功";
}else{
    echo "修改失败";
}
 
$rt = PostList::model()->updateAll(array("status"=>"1"),"staff_postid=:staff AND host_postid=:host",array(":staff"=>$staff_postid,":host"=>$host_postid));

Post::model()->updateByPk($pk,$attributes,$condition,$params);
$count=Post::model()->updateByPk(1,array("username"=>"jack","password"=>"jack"));

$count=Post::model()->updateByPk(array(1,2),array("username"=>"jack1","password"=>"jack1"),"username=:name",array(":name"=>"jack"));

if($count > 0){
    echo "修改成功";
}else{
    echo "修改失败";
}
 
Post::model()->updateCounters($counters,$condition,$params);

$count=Post::model()->updateCounters(array("status"=>1),"username=:name",array(":name"=>"jack"));

if($count > 0){
    echo "修改成功";
}else{
    echo "修改失败";
}
//array("status"=>1)代表数据库中的post表根据条件username="jack",查询出的所有结果status字段都自加1

六、删除的方法

//deleteAll
Post::model()->deleteAll($condition,$params);
$count = Post::model()->deleteAll("username=:name and password=:pass",array(":name"=>"jack",":pass"=>"jack"));
$count = Post::model()->deleteAll("postid in("1,2,3")");//删除postid为这些的数据
if($count>0){
    echo "删除成功";
}else{
    echo "删除失败";
}
 
//deleteByPk
Post::model()->deleteByPk($pk,$condition,$params);
$count = Post::model()->deleteByPk(1);
$count =Post::model()->deleteByPk(array(1,2),"username=:name",array(":name"=>"jack"));
if($count>0){
    echo "删除成功";
}else{
    echo "删除失败";
}}

七、执行原生的SQL语句

$sql = "select t.*, t1.userphone, t1.truename, t1.usermail from {{member_contact}} t left join {{member}} t1 on t.userid = t1.userid where t.contactid in (1,2,3)";
$arrRows=Yii::app()->db->createCommand($sql)->query();
foreach ($arrRows as $k => $v){
  
}

八、事务处理 【多表更新插入操作请使用事务处理】

$transaction = Yii::app()->db->beginTransaction();
try{
                $arrOrderProfile = array(
                    "orderid"     => $orderId,
                    "userip"      => $userip,
                    "contactid" => $contactId,
                    "updatetime"=> $now
                );
                $modelOrderProfile = new RepairOrderProfile();
                $modelOrderProfile->attributes = $arrOrderProfile;
                if(!$modelOrderProfile->save()){
                    throw new CException("维修订单生成失败,通知事务回滚");
                }
                $recordCounter = Counter::model()->updateByPk(1,array("max_id"=>$orderId));
                if($recordCounter <= 0 )
                    throw new CException("订单计数器更新失败,通知事务回滚");
                $transaction->commit(); //提交事务会真正的执行数据库操作
}catch(Exception $e){
    file_put_contents("action.log", $e->getCode().":".$e->getMessage()."--".date("Y-m-d H:i:s",time()),FILE_APPEND);
    $transaction->rollback();
   
}  

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28773.html

相关文章

  • 使用YII 做开发的总结

    摘要:也提供了命名查询的方式,比如需要获取最近一个月内发布的篇文章,如果经常性的用到这个查询,可以使用命名查询的方式来写。 这两天用YII开发了用户管理的功能,以前虽然也用YII框架开发过一些功能,但是总感觉对YII的使用还不是很熟练。 这次真正动手之前,先复习了一遍 yii-guide-1.1.14.pdf 这本书,上次看的时候太过于粗略了,这次仔仔细细的阅读了一遍。 说一下最直观的感受 ...

    mykurisu 评论0 收藏0
  • Yii的修行之路 - Active Record 活动记录

    摘要:建立关联关系后,通过可以获取一个对象的数组,该数组代表当前客户对象的订单集。定义关联关系使用一个可以返回对象的方法,对象有关联上下文的相关信息,因此可以只查询关联数据。基于表外键定义关联关系是最佳方法。 简介 Yii 在操作数据库方面提供了一个十分强大的类库来支撑整个框架业务的运转,这就是 Active Record (活动记录,以下简称AR)。 基本概念 AR类提供了一个面向对象的接...

    HmyBmny 评论0 收藏0
  • yii2 ActiveRecord多表关联以及多表关联搜索的实现

    摘要:今天把这个问题讲明白了,看看是怎么个多表关联以及如何去优化这个关联。现需要在列表展示表的来源渠道,且该渠道可搜索。关联表字段增加查询中的搜索模型也是通过实现的,该模型通过控制着哪个字段可搜索,哪个字段不可搜索。 作者:白狼 出处:http://www.manks.top/yii2_many_ar_relation_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留...

    venmos 评论0 收藏0
  • RageFrame 一个 Yii2 + AdminLET 免费开源多商户通用后台管理系统

    摘要:极致的插件机制,系统内的系统,安装和卸载不会对原来的系统产生影响强大的功能完全满足各阶段的需求,支持用户多端访问后台微信前台等,系统中的系统。多入口模式,多入口分为后台前端,微信,对内接口,对外接口,不同的业务,不同的设备,进入不同的入口。 RageFrame 2.0 为二次开发而生,让开发变得更简单 项目地址:https://github.com/jianyan74/... 前言 这...

    sunny5541 评论0 收藏0

发表评论

0条评论

malakashi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<