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