Updating a record using DAO in yii
$connection1=Yii::app()->db;$sql1="update inwtrans set out_section='$sname',out_time='$tdt' where id=$max";
$command1=$connection1->createCommand($sql1)->execute();
Similarly Inserting a record example usig DAO is below
$connection=Yii::app()->db;
$sql="insert into inw_trans ( inw_no, inw_year, sec_code, sub_scode, in_time, status, in_section, ipadd) values ($inwno,'$inwyr',$sec_code,$subsec,'$tdt','O','$sname','$ipadd')";
$command=$connection->createCommand($sql)->execute();
Error handling code to perform the transactions :
$prevyr = intval(date('Y')-1);$connection=Yii::app()->db;
$transaction = $connection->beginTransaction();
try {
$sql="insert into inward2(fms_rno,ref_no,uid) select fms_rno,ref_no,uid from inward where inw_year='$prevyr';";
$connection->createCommand($sql)->execute();
$sql1="delete from inward where inw_year='$prevyr'";
$connection->createCommand($sql1)->execute();
$sql2="alter table inward auto_increment=1";
$connection->createCommand($sql2)->execute();
$sql3="insert into inw_trans2(inw_no,sec_code,sub_scode) select inw_no, sec_code, sub_scode from inw_trans where inw_year='prevyr'";
$connection->createCommand($sql3)->execute();
$sql4="delete from inw_trans where inw_year='$prevyr'";
$connection->createCommand($sql4)->execute();
$sql5="alter table inw_trans auto_increment = 1;";
$connection->createCommand($sql5)->execute();
$transaction->commit();
}catch (Exception $e) {
$transaction->rollBack();
}
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();
No comments:
Post a Comment