Hello all,
sorry if my posting is silly.
I’m working on project that just have one database, and base on several articles that points DAO is more lightweight and pretty straightforward (Looks like ADO for me).
I have write a simple class, looks like this:
class DBHelper extends CActiveRecord
{
const execute = 0;
const query = 1;
const queryAll = 2;
const queryRow = 3;
const queryColumn = 4;
const queryScalar = 5;
private static $_command = null;
//make this class into singelton
public static function GetInstance()
{
if(!(self::$_command instanceof DBHelper))
{
self::$_command =Yii::app()->db;
}
return self::$_command;
}
public function SelectRecord($query, $executeMod, $fetchAssociative=true, $params=array())
{
$command = self::GetInstance()->createCommand($query);
switch($executeMod)
{
case 0:
$result = $command->execute($params=array());
break;
case 1:
$result = $command->query($params=array());
break;
case 2:
$result = $command->queryAll($fetchAssociative, $params=array());
break;
case 3:
$result = $command->queryRow($fetchAssociative, $params=array());
break;
case 4:
$result = $command->queryColumn($params=array());
break;
case 5:
$result = $command->queryScalar($params=array());
break;
default:
$result = null;
throw new Exception('Exception from DBComponent Class....');
break;
}
return $result;
}
public function TExecute($query)
{
$transaksi = self::GetInstance()->beginTransaction();
try
{
self::GetInstance()->createCommand($query);
self::GetInstance()->execute();
$transaksi->commit();
} catch(Exception $e)
{
$transaksi->rollback();
}
}
public function TArrExecute($query)
{
$connT = Yii::app()->db();
$transaksi = Yii::app()->db->beginTransaction();
try
{
foreach($query as $q)
{
self::GetInstance()->createCommand($q);
self::GetInstance()->execute();
}
$transaksi->commit();
} catch(Exception $e)
{
$transaksi->rollback();
}
}
}
P/S: that code is not tested…
Is that a good practice to write a wrapper for DAO? or maybe i just use the active record or Query builder?