I would NOT like to use activerecord but run my own SQL statements using Yii, How do i do that ?
Also, what are the functions for iterating through a result set once i execute a query ?
I would NOT like to use activerecord but run my own SQL statements using Yii, How do i do that ?
Also, what are the functions for iterating through a result set once i execute a query ?
Set Following Controller where You can perform
or
May be used in View OR Model as per Your requirement
<?php
$SQL="SQL Statemet"
$connection=Yii::app()->db;
command=$connection->createCommand($sql);
$rowCount=$command->execute(); // execute the non-query SQL
$dataReader=$command->query(); // execute a query SQL
?>
Example of a query which return an array
$list= Yii::app()->db->createCommand('select * from post')->queryAll();
$rs=array();
foreach($list as $item){
//process each item here
$rs[]=$item['id'];
}
return $rs;
if you want to bind some params:
$list= Yii::app()->db->createCommand('select * from post where category=:category')->bindValue('category',$category)->queryAll();
if you just want to run a query return nothing return:
Yii::app()->db->createCommand('delete * from post')->query();
Hope this help
Best regard
Thank you, your answer worked for me.