2016-04-19 13:44:13 0 评论 Yii 2.0 Boy.Lee

在Yii 2.0框架中使用DAO

在Yii 2.0 框架中除了大家经常使用的 AR, 在一些特殊场景需要使用DAO来操作数据库, 更直接更快速, 下面就是一组DAO常用功能示例代码

 

//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();