Home >Backend Development >PHP Tutorial >CakePHP 2.x upgrade to 3.0
CakePHP 2.x => 3.x Upgrade Easy
1. Controller layer
1.find('list')
In the original 2.x, this method implements key=>value by specifying the 'fields' in the option. In 3.x, it is replaced by the 'keyField' and 'valueField' in the option. The return value needs to be converted using toArray()
3.x has completely abandoned these two parameters, and use find()->first() and find()->count( ) to replace
4.findById
**? No relevant document found?**
The format of findById in 2.x is ($id, array $fields)
3.x may have only one parameter ($id), which needs to be passed select(array $fields) method to filter data
2.x uses initialize(Controller $Controller) to get the Controller that calls the Component
3.x changes the parameters of initialize to initialize(array $config), and uses $controller = $ this->_registry->getController();Get
3.x abandoned this Component and replaced it with $this->request->session()
1.Associations
2.x realizes association by defining attributes
3.x needs to call $this->belongsTo, $this->hasMany, $this->belongsToMany, $this in the initialize method in Table ->addAssociations and other methods to achieve
2.x implements data validation by defining the validate attribute
3.x needs to define the validationDefault(Validator $validator) method in Table to achieve
This attribute was abandoned in 3.x. If you need to use it, you need to add a method to the Entity
For example, you need to define the virtual field full_name in table A
First, you need to add the protected function _getFullName()
in the Entity of table A
Add full_name to $_accessible
When using A->find, the value can only be obtained through object->property and cannot be obtained through arrays
4.field()
**?No relevant document found?**
3.x may have abandoned this method and can be replaced with the get() method
5.ClassRegistry::init()
3.x This method is abandoned and needs to be replaced with TableRegistry::get()
6.Mysql alias setting
2.x You only need to add as to the fields attribute, such as 'fields'=>array('id as table_id')
3.x If you define an alias, you need to add a key, such as 'fields '=>['table_id'=>'id']
3. View layer
(View layer, moved from View folder of 2.x to Template of 3.x)
1.$this->Html->url
In 3.x, use $this->Url->build instead
4. Bug
1. Associations
When querying hasMany table association, if you need to specify fields, must you add association keys to the fields? ? ?
This upgrade guide will continue to be updated~~~~~~
The above introduces the upgrade of CakePHP 2.x to 3.0, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.