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。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值