愛悠閑 > yii CActiveRecord 查詢

yii CActiveRecord 查詢

分類: YII  |  作者: yuhui_fish 相關  |  發布日期 : 2014-09-08  |  熱度 : 249°

find()

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


配合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

$post=Post::model()->find(array(
    'select'=>'title',
    'condition'=>'postID=:postID',
    'params'=>array(':postID'=>10),
));
// find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);


findByAttributes()

$post=Post::model()->findByAttributes($attributes,$condition,$params);

$checkuser = user_field_data::model()->findByAttributes(
    array('user_id' => Yii::app()->user->user_id, 'field_id' => $fieldid));

$user_field_data = user_field_data::model()->findAllByAttributes(
    $attributes = array('user_id' => ':user_id'),
    $condition = "field_id in (:fields)",
    $params = array(':user_id' => Yii::app()->user->user_id, ':fields' => "$rule->dep_fields"));
findBySql($sql,$params)
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);
//例子
user_field_data::model()->findBySql(“select id from user_field_data where user_id = :user_id and field_id = :field_id “, array(‘:user_id’ => $userid,’:field_id’=>$fieldid));
//此時回傳的是一個對象


快乐彩中奖说明