Yii command for db query

$sql = "SELECT ".$requireID." FROM ".$tableName." where ".$ID." ='".$val."';";
$cmd = Yii::app()->db->createCommand($sql);
result = $cmd->queryRow();

-----------------------------------------------------

$member = Person::model()->findByAttributes(
array()
, " delete_flg = :deleteFlg AND user_is_locked = :isLocked ",
array(
":deleteFlg" => 0,
":isLocked" => 0,
));

--------------------------------------

$member = BaseMember::model()->findByPk(
                     $memberId,
               ' delete_flg=:flag AND user_is_locked=:flag ',
                array(':flag' => 0));


-----------------------------------------------------
$sql = "SELECT name FROM people";
$cmd = Yii::app()->db->createCommand($sql);
$result = $cmd->queryRow();
$result2 = $cmd->queryAll();

------------------------------------------------------
$criteria = array("condition"=>"(status=1 AND del_flg=0)");
$result = Modelname ::model()->findAll($criteria);

----------------------------------------------
 $sql = Yii::app()->db->createCommand()
->select(array('rt.*','ut.*'))
->from('riders_tweets rt')
->leftJoin('user_twitter ut','ut.tw_id_str = rt.user_id_str')
->where(array('and','rt.del_flg=0','rt.status=1','rt.user_id_str="'.$user_id_str.'"'))
->limit($limit)
->offset($offset)
->order('rt_id_str DESC')
->queryAll();
return $sql;
//array result;
---------------------------

$criteria = new CDbCriteria();
$criteria->select= "t.* , categories_post.category_name";
$criteria->alias = 't';
$criteria->join = 'LEFT JOIN categories_post ON categories_post.category_id = t.sch_category_id';
$criteria->condition= $where;
$criteria->order = 't.schedules_date DESC, t.display_order DESC';
//$criteria->group = 't.sch_category_id';
//$count = Modelname::model()->count($criteria);
$data = Modelname::model()->findAll($criteria);
return $data;


if(is_array($data)) {
                  foreach($data $key=>$val) {
                        $Id = CHtml::encode($val->id);
                        ${field_name} = CHtml::encode($val->{field_name});
                   }
}
----------------------------------
$criteria = new CDbCriteria;
$criteria->condition='post_id=:post_id AND status=:status';
$criteria->params=array(':post_id'=>8, ':status'=>'active');
$post = Post::model()->find($criteria);
---------------------------------------

1. Supply a $condition as string:
Person::model()->findByAttributes(
    array('first_name'=>$firstName,'last_name'=>$lastName),
    'status=1'
);


2. Supply a $condition as string that contains placeholder and $params as array with placeholder values:
Person::model()->findByAttributes(
    array('first_name'=>$firstName,'last_name'=>$lastName),
    'status=:status',
    array(':status'=>1)
);


3. Supply a $condition as a CDbCriteria:
$criteria=New CDbCritieria;
$critieria->condition='status=1';
Person::model()->findByAttributes(
    array('first_name'=>$firstName,'last_name'=>$lastName),
    $criteria);


4. Supply a $condition as array with property values for CDbCriteria:
Person::model()->findByAttributes(
    array('first_name'=>$firstName,'last_name'=>$lastName),
    array(
        'condition'=>'status=:status', 
        'params'=>array(':status'=>1)
    )
);

-----------------------------

Updates
---------------
$user = User::model()->findByPk(1);
$user->lastLogin = date('Y-m-d H:i:s');
$user->update(array('lastLogin'));

$person = Person::model()->findByAttributes(
    array('first_name'=>$firstName,'last_name'=>$lastName));
$person->notification = 0;
$person->update(array('notification'));

Links
chtml : http://www.yiiframework.com/wiki/48/by-example-chtml

Share this

Related Posts

Previous
Next Post »