CakePHP 2.x 升级3.0,cakephp2.x升级3.0
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Several methods for Conda to upgrade the Python version require specific code examples. Overview: Conda is an open source package manager and environment management system for managing Python packages and environments. During development using Python, in order to use a new version of Python, we may need to upgrade from an older Python version. This article will introduce several methods of using Conda to upgrade the Python version and provide specific code examples. Method 1: Use the condainstall command

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

Validator can be created by adding the following two lines in the controller.

Many friends expressed that they want to know how to upgrade to a professional account in Xiaohongshu. Here I will introduce the operation method. If you are interested, come and take a look with me. Open the "Little Red Book" APP on your mobile phone, click the "My" option in the lower right corner after entering it, then find the "three horizontal lines" icon in the upper left corner of the My page and click to open it. 2. A menu page will pop up, in which click to select the "Creation Center" item to enter. 3. Next, find "More Services" in the options under "Creation Services" on the page you enter, and click on it to enter. 4. After the page jumps, click "Open a Professional Account" in the options under "Author Capabilities". 5. Finally, the Xiaohongshu Professional Account will be introduced on the entered page. Click "

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

On June 21, Huawei Developer Conference 2024 (HDC2024) gathered again in Songshan Lake, Dongguan. At this conference, the most eye-catching thing is that HarmonyOSNEXT officially launched Beta for developers and pioneer users, and comprehensively demonstrated the three "king-breaking" innovative features of HarmonyOSNEXT in all scenarios, native intelligence and native security. HarmonyOSNEXT native intelligence: Opening a new AI era After abandoning the Android framework, HarmonyOSNEXT has become a truly independent operating system independent of Android and iOS, which can be called an unprecedented rebirth. Among its many new features, native intelligence is undoubtedly the new feature that can best bring users intuitive feelings and experience upgrades.
