CakePHP 2.x 升级3.0,cakephp2.x升级3.0
CakePHP 2.x => 3.x 升级手顺
一、Controller层
1.find('list')
原2.x中该方法通过指定option中的'fields'来实现key=>value
3.x通过option中的'keyField'和'valueField'来替换,返回值需要用toArray()进行转换
2.find('first')、find('count')
3.x中已经完全废弃掉这两个参数,而使用find()->first()以及find()->count()进行替换
3.find查询关联表时,必须添加'contain'属性,Cake 3.x 默认不会将表关联在一起
4.findById
**?没有找到相关文档?**
2.x中findById格式为($id,array $fields)
3.x中可能只有一个参数($id),需要通过select(array $fields)方法来筛选数据
5.Component->initialize()
2.x通过initialize(Controller $Controller)获取调用该Component的Controller
3.x改变了initialize的参数变为initialize(array $config),通过$controller = $this->_registry->getController();获取
6.SessionComponent
3.x废弃掉了该Component而改用$this->request->session()替换
二、Model层
1.Associations
2.x通过定义属性来实现关联
3.x需要在Table中的initialize方法中调用$this->belongsTo、$this->hasMany、$this->belongsToMany、$this->addAssociations等方法来实现
2.Validation
2.x通过定义validate属性实现数据验证
3.x需要在Table中定义validationDefault(Validator $validator)方法来实现
3.virtualFields
3.x中废弃了这个属性,如需使用,需要在Entity中追加方法
例如 需要A表定义虚拟字段 full_name
首先要在A表的Entity中追加 protected function _getFullName()
然后将 full_name 添加到$_accessible中
使用时A->find 后该值只可通过object->property的方式获取,无法通过数组方式获取
4.field()
**?没有找到相关文档?**
3.x可能废弃了该方法可以用get()方法替换
5.ClassRegistry::init()
3.x废弃该方法需要用TableRegistry::get()替换
6.Mysql别名设定
2.x只需要在fields属性中加入as即可 如 'fields'=>array('id as table_id')
3.x如定义别名需要用加入key 如 'fields'=>['table_id'=>'id']
三、View层
(View层,从2.x的View文件夹搬迁到3.x的Template)
1.$this->Html->url
3.x中用$this->Url->build替代
四、Bug
1. Associations
hasMany的表关联查询时,如果需要指定fields则必须在fields中加入关联键???
此升级手顺会持续更新中~~~~~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
