YII database query, YII Database

Source: Internet
Author: User

YII database query, YII Database

$ UserModel = User: Model (); $ userModel-> count ($ condition, $ params); $ userModel-> countByAttributes ($ attributes, $ condition); $ userModel-> countByAttributes ($ attributes, $ condition, $ params); $ userModel-> countBySql ($ SQL, $ params); // $ userModel = User: Model (); $ userModel-> find (); $ userModel-> find ($ condition, $ params ); $ userModel-> findByPk ($ pk); $ userModel-> findByPk ($ pk, $ condition); $ userModel-> findByPk ($ pk, $ condition, $ params ); $ userModel-> findByAttributes ($ attributes); $ userModel-> findByAttributes ($ attributes, $ userModel); $ userModel-> findByAttributes ($ attributes, $ userModel, $ params ); $ userModel-> findBySql ($ SQL); $ userModel-> findBySql ($ SQL, $ params); // The returned value is an array of objects. If it is NULL, NULL is returned, for a record $ findResult = $ userModel-> find ('islock =: islock and phone_status =: phone_status ', array (': islock' => 1 ,': phone_status '=> 1); echo $ findResult-> phone_status. "<br/>"; var_dump ($ findResult-> attributes); if ($ userModel-> find ('user _ id = 260 ') {echo 'Find ';} else {echo 'no find ';}


Parameter explanation: the above returns a record. If you want to return multiple data records

$ Condition is the where clause in a general SQL statement. It is a single character.

$ Params is an array that assigns values to placeholders.

Query all data:

        $userinfo= $userModel->findAllByAttributes(array('islock'=>1,'phone_status' => 0));        foreach($userinfo as $k => $v){            echo $v->user_id."  ".  $userinfo[$k][user_id]."<br />";        }

The result is a two-dimensional array.

How to combine CDbcriteria and Model Objects

        $criteria = new CDbCriteria;         if($condition) $criteria->condition = $condition;        $criteria->offset = $offset;        if($order) $criteria->order = $order;        if($limit!="ALL") $criteria->limit = $limit;        if(!empty($more_criteria)){            $criteria->mergeWith($more_criteria);        }        $Model = new $modelName;        if(!empty($with_array)){            $result = $Model->with($with_array)->findAllByAttributes($attributes,$criteria);        }else{            $result = $Model->findAllByAttributes($attributes,$criteria);        }





Use php yii framework to write and query data in the database

There are two methods. One is to associate the user model with the common model.
Second, you can use DAO to directly write SQL to read data.
 
Yii database query and data output problems: currently tables a and B need to query the content released on the x Day Of a and the content released on the x Day of B, and then output

Select release content from a where x
Union all
Select release content from B where x
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.