YII1升级到YII2的注意事项
来源:互联网 发布:淘宝天天特价报名条件 编辑:程序博客网 时间:2024/05/17 06:20
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.
- YII1升级到YII2的注意事项
- Yii1.1应用升级到Yii2.0的一些注意点
- YII1.1升级到2.0笔记
- Yii2.0 对比 Yii1.1 的重大改进
- 升级VC7项目到VC8的注意事项
- 升级VC7项目到VC8的注意事项
- 升级VC7项目到VC8的注意事项
- 升级SQLSERVER6.5到2000的注意事项
- 升级VC7项目到VC8的注意事项
- SQL Server2000升级到2005的注意事项
- 升级VC7项目到VC8的注意事项
- gitlab 升级到5.0的严重注意事项
- 升级到Scriptcase 8.0的注意事项
- 1.1-Yii2.0 Yii2.0 对比 Yii1.1 的重大改进
- Yii2使用Cookie的注意事项
- Yii2使用Cookie的注意事项
- 从 Yii 1.1升级到 Yii2
- GitLab升级到5.1注意事项
- 最长递增子序列
- 分享txt转换pdf格式具体转换方法
- 在一个Activity 里面去关闭另一个Activity或者多个Acitivity
- 排序--看一眼就忘不了
- 详细透彻的分析DM9000网卡驱动程序(1)
- YII1升级到YII2的注意事项
- 职场新人,你进入职场应该规划的事
- Android每日范例——TextView文字内容更改
- LeetCodeOJ.Longest Substring Without Repeating Characters
- windows下python实现截图功能
- Android GUI之Window、WindowManager
- C++ 析构函数
- FMDB
- IBeacon技术