Yii DAO consists: CDbConnection, CDbCommand, CDbDataReader and CDbTransaction. Post of examples

$connection=new CDbConnection($dsn,$username,$password);
$connection->active=false;  // close connection
to use, configure in a db app component in the app config
            'emulatePrepare'=>true,  // needed by some MySQL installations
//if done can use as
$rowCount=$command->e xecute();
//if SELECT   
// if return all rows of result  
// go through every row of data
foreach($dataReader as $row) { ... }
// get all in a single array
//4th part of DAO - transaction
$transaction = $connection->beginTransaction();
    $connection->createCommand($sql1)->e xecute();// etc
catch(Exception $e) // an exception is raised if a query fails
//Binding Parameters
$sql="INSERT INTO user (username, email) VALUES(:username,:email)";
$command->e xecute();
// possible for Binding Columns
//create query builder and e xecute with DAO
$user = Yii::app()->db->createCommand()
    ->select('id, username, profile')
    ->from('user u')
    ->join('profile p', 'u.id=p.user_id')
    ->where('id=:id', array(':id'=>$id))
// using CDbCriteria with DAO, no AR used
$criteria = new CDbCriteria;//config then
$builder = new CDbCommandBuilder(Yii::app()->db->gets chema());
//use table name and criteria
$command = $builder->createFindCommand('product', $criteria); 
$productIds = $command->queryAll();
//CArrayDataProvider implements a data provider based on a raw data array
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
// or using: $rawData=User::model()->findAll();
$dataProvider=new CArrayDataProvider($rawData, array(
             'id', 'username', 'email',
// $dataProvider->getData() will return a list of arrays.
$criteria=new CDbCriteria(array(
'join'=>'left join category_description cs ON c.category_id=cd.category.id',
'condition'=>'c.category_id=:category and cd.site_id=:site',
foreach($dataReader as $row)
//обработка результата

One comment

Denny says:
Thursday, October 2, 2014 11:11 AM
Thiss article gives clear idea in favor of thee new visitors of blogging,
that really howw to do blogging.

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.