Use DAO in Yii 2.0 Framework

We usually use AR in Yii 2.0 it's quick and easy, but for some condition we need use DAO for Database operate, it's direct and faster, follow are some demo code

 

//Query All
$topics = Yii::$app->db->createCommand('SELECT * FROM yiilib.topics')->queryAll();

//Query One
$topics = Yii::$app->db->createCommand('SELECT * FROM yiilib.topics')->queryOne();

//Get Count
$count = $db->createCommand('SELECT COUNT(*) FROM yiilib.topics')->queryScalar();

//DO topic.commentCount ++
//SQL for update topic.commentCount ++
$sql = 'update yiilib.topic set commentCount = commentCount+1 where topic_id=:topicID';

//create command and run the SQL
\Yii::$app->db->createCommand($sql)->bindValue(':topicID', $this->com_topic_id)->execute();