1、数据库访问,如果直接用sql,需要采用绑定参数的方式。比较安全。
Yii1:
$con = Yii::app()->db; $sql = "SELECT title, img from goods_pic where id = :id"; $command = $con->createCommand($sql)->bindParam(':id', $id); $result = $command->queryRow();
2、快速区分Yii1和Yii2
Yii1:
Yii::app()
Yii2:
Yii::$app()
3、使用日志
Yii1:
Yii::log($query, CLogger::LEVEL_ERROR, $category = 'application');
Yii2:
use yii\log\Logger; Yii::getLogger()->log($message, Logger::LEVEL_ERROR, $category = 'application');
查看日志在这个文件:
protected/runtime/application.log
关于日志的配置:
'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( 'class' => 'CFileLogRoute', 'levels' => 'error, warning',//,info,trace ), // uncomment the following to show log messages on web pages /* array( 'class'=>'CWebLogRoute', ), */ ), ),
记录数据库执行日志:
'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( 'class' => 'CFileLogRoute', 'levels' => 'error, warning, trace',//,info,trace ), // 记录数据库日志 array( 'class' => 'CWebLogRoute', 'levels' => 'trace', //级别为trace 'categories' => 'system.db.*' //只显示关于数据库信息,包括数据库连接,数据库执行语句 ), // uncomment the following to show log messages on web pages /* array( 'class'=>'CWebLogRoute', ), */ ), ),
4、Controller的调用时的区别
比如controller文件名为AbcUsersController.php
函数为actionIndex
Yii1:
index.php?r=abcUsers/index&page=1
Yii2:
index.php?r=abc-users/index&page=1
对应的view下的目录也类似。需要命名为abc_users。