Home php教程 php手册 Yii CDbCriteria

Yii CDbCriteria

Jun 13, 2016 am 09:34 AM
php

注:$c = new CDbCriteria();是ActiveRecord的一种写法,使ActiveRecord更加灵活,而不是手册中DAO(PDO)和Query Builder。 

这是Yii CDbCriteria的一些笔记和常用用法: 

一、一个sql拼装的情况

    $criteria = new CDbCriteria;           

    $criteria->addCondition("id=1"); //查询条件,即where id = 1     

    $criteria->addInCondition('id'array(1,2,3,4,5)); //代表where id IN (1,23,,4,5,);     

    $criteria->addNotInCondition('id'array(1,2,3,4,5));//与上面正好相法,是NOT IN     

    $criteria->addCondition('id=1','OR');//这是OR条件,多个条件的时候,该条件是OR而非AND     

    $criteria->addSearchCondition('name''分类');//搜索条件,其实代表了。。where name like '%分类%'     

    $criteria->addBetweenCondition('id', 1, 4);//between 1 and 4      

         

    $criteria->compare('id', 1);    //这个方法比较特殊,他会根据你的参数自动处理成addCondition或者addInCondition,     

                                    //即如果第二个参数是数组就会调用addInCondition     

        

    $criteria->addCondition("id = :id");     

    $criteria->params[':id']=1;     

      

//属性方式     

    $criteria->select = 'id,parentid,name'//代表了要查询的字段,默认select='*';     

    $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; //是否唯一查询    




实例: 

Php代码  

$criteria = new CDbCriteria();   

$criteria->select = 'table_name,model_id,sum(amount) total';   

$criteria->group = 'table_name,model_id';   

$criteria->addCondition("$nIdcId=4");//也可以$criteria->condition = "$nIdcId=4";   

$aResult = accessory_info::model()->findAll($criteria);   

  

  

$c = new CDbCriteria();   

$c->select = 't.id, t.created_at, t.outsource_id, t.user_id, t.operate, t.content';   

$c->join = 'LEFT JOIN outsource ON outsource.id=t.outsource_id';   

$c->condition = 'outsource.idc_id IN(' . implode(','$idc_ids)  . ')';   

  

if($last_log_id) {   

    $c->condition .= " AND t.id > $last_log_id";   

}   

  

$c->limit = 20;   

$c->order = 't.id DESC';   

  

$logs = OutsourceProcessLog::model()->findAll($c);  



批注: 
1,与DAO方式结果区别是:DAO方式  数组中的每一个元素仍是数组。而通过CDbCriteria方式,数组中的元素是对象。 
2,即使该功能很强大,仍有一些需求不能满足,此时仍需sql语句。比如select avg(num) amount from ****。 


二、mergeWith的情况

//在ActiveRecord中,增加条件限制   

$this->getDbCriteria()->mergeWith(array(   

    'condition'=>"idc_id IN ($ids)",   

)); 

 

原文转自百度空间《Yii CDbCriteria 常用方法》

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles