Yii快速入門及Yii1與Yii2對比



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。







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM