YII1升级到YII2的注意事项

YII1升级到YII2版本升级的要点及部分语法的区别(包含路由器区分大小写的解决方案)。

一、 获取配置参数

1.x:

$fileUrl = Yii::app()->params['downloadUrl'];

2.0:

$fileUrl = Yii::$app->params['downloadUrl'];
二、 调用模型自带函数

1.x:

$fileInfo = App::model()->find('app_id=:app_id', array(
':app_id' => $app_id
));

2.0:
先在头部引入namespace:use app\models\App;

$appObj = new App();

查询单条信息:

$fileInfo = $appObj->find()->where([‘app_id’ => $app_id])->one();

查询多条信息:

$fileInfo = $appObj->find()->where([‘app_id’ => $app_id])->all();
三、 调用模型自定义函数

1.x:

$sipInfo = ServiceInfo::model()->getSipNo($guid);

2.x:
先在头部引入namespace:use app\models\ServiceInfo;

$serviceInfoObj = new ServiceInfo;
$sipInfo = $serviceInfoObj->getSipNo($guid);
四、 Controller新增数据

1.x:

$userObj = new User;
$userObj->attributes = $attributes;
$userObj->save();

2.0

$userObj = new User;
$userObj->isNewRecord = true;
$userObj->save();
五、 模型中新增数据

1.x:

$userObj = new User;   //实例化当前模型
$userObj->attributes = $attributes;
$userObj->save();

2.0:

$this->isNewRecord = true;
$this->attributes = $attributes;
$this->save();
六、 调用存储过程

1.x:

$sql = “select * from device.sp_device_register()”;
$command = Yii::app()->db->createCommand($sql);
$result = $command->queryAll();

2.0:

$sql = “select * from device.sp_device_register()”;
$command = $this->getDb()->createCommand($sql);
$result = $command->queryAll();
七、 rules验证规则

1.x:

return array(
array(‘username, password’, ‘safe’);
);

2.0:

return [
[[‘username’, ‘password’], ‘safe’]
];
八、 Model的父类

1.x:
模型继承ActiveRecord
2.0:
依然继承ActiveRecord
但需要引入命名空间:yii\db\ActiveRecord; 注意不要引入错误 !

九、 路由大小写区分

1.x:

'urlManager' => [
      'caseSensitive' => false,    //路由是否区分大小写
]

2.0:
由于考虑SEO优化问题,此版本取消路由区分大小写的选项,如需不区分大小写,要改动以下内容:
Controller:
vendor/yiisoft/yii2/base/Module.php line 554
这里写图片描述

line 567
这里写图片描述

Action:
vendor/yiisoft/yii2/base/Controller.php line 220-228 红框内是修改部分
这里写图片描述

十、 Migrations数据库迁移

1.x:

'commandMap'=>array(
     'migrate'=>array(
         'class'=>'system.cli.commands.MigrateCommand',
            'migrationPath'=>'application.migrations',
            'migrationTable'=>'tbl_migration',
            'connectionID'=>'db',
            'templateFile'=>'application.migrations.template',
     ),
     ......
)

2.0:

'controllerMap' => [
     'migrate' => [
          'class' => 'yii\console\controllers\MigrateController',
          'migrationTable' => 'migration.api_migration',
     ],
],

1、 migration.api_migration会自动创建,但是要提前建立好模式
2、 migration默认使用配置文件中的components[‘db’],可以自定义
3、 如果提示找不到驱动(driver),需要检查php的路径是否正确及是否开启pgsql扩展

十一、 Redis配置与使用

1.x:

        'redis_cache' => array (
            'class' => 'system.caching.CRedisCache',
            'hostname' =>'10.0.0.232',
            'port'=>6379,
            'password'=>'Doordu2015!!',
            'database'=>0
        )

2.0:

        'redis_cache' => [  //redis_cache这个名字可以自定义
            'class' => 'yii\redis\Connection',
            'hostname' => '10.0.0.232',
            'port' => 6379,
            'password' => 'Doordu2015!!',
            'database' => 0
        ],

需要在Yii2.0中安装扩展,以下是三种安装方式:
1、 直接下载(https://github.com/yiisoft/yii2-redis)、改名为yii2-redis放入vendor目录下,最后在vendor/yiisoft/extensions.php中加入以下配置:
这里写图片描述
2、 在composer.json文件中加入:”yiisoft/yii2-redis”: “~2.0.0”
3、 php composer.phar require –prefer-dist yiisoft/yii2-redis

十二、 下载文件

1.x:

Yii::app()->request->sendFile($path, file_get_contents($path));

2.0:

Yii::$app->response->sendFile($path);

注:sendFile后不能加入任何断点,否则无法下载。
Note that this method only prepares the response for file sending. The file is not sent until send() is called explicitly or implicitly. The latter is done after you return from a controller action.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值