CakePHP 2.x upgrade to 3.0, cakephp2.x upgrade to 3.0_PHP tutorial

WBOY
Release: 2016-07-13 09:57:11
Original
1132 people have browsed it

CakePHP 2.x upgrade to 3.0, cakephp2.x upgrade to 3.0

CakePHP 2.x => 3.x upgrade instructions

1. Controller layer

1.find('list')
In the original 2.x, this method implements key=>value
by specifying the 'fields' in the option. 3.x uses the 'keyField' and 'keyField' in the option. 'valueField' to replace, the return value needs to be converted using toArray()

2.find('first'), find('count')
3.x has completely abandoned these two parameters, and use find()->first() and find()- >count() to replace

3. When find queries related tables, you must add the 'contain' attribute. Cake 3.x will not associate tables together by default

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), you need to filter the data through the select(array $fields) method

5.Component->initialize()
2.x gets the Controller that calls the Component through initialize(Controller $Controller)
3.x changed the parameter of initialize to initialize(array $config) , get

through $controller = $this->_registry->getController();

6.SessionComponent
3.x abandoned this Component and used $this->request->session() to replace

2. Model layer

1.Associations
2.x realizes association by defining attributes
3.x needs to call $this->belongsTo, $this->hasMany, $this- in the initialize method in Table >belongsToMany, $this->addAssociations and other methods to achieve

2.Validation
2.x implements data validation by defining the validate attribute
3.x needs to define the validationDefault(Validator $validator) method in Table to implement

3.virtualFields
This attribute was abandoned in 3.x. If you need to use it, you need to add the method
in Entity. For example, you need to define the virtual field full_name in table A
. First, you need to define the virtual field full_name in table A in the Entity. Append protected function _getFullName()
   Then add full_name to $_accessible
  When using A->find, the value can only be obtained through object->property and cannot be obtained through array

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 only needs to add as in the fields attribute, such as 'fields'=>array('id as table_id')
3.x needs to define an alias Use adding key such as 'fields'=>['table_id'=>'id']

3. View layer

(View layer, moved from the View folder of 2.x to the Template of 3.x)

1.$this->Html->url
In 3.x, use $this->Url->build instead of

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~~~~~~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984441.htmlTechArticleCakePHP 2.x upgrade to 3.0, cakephp2.x upgrade to 3.0 CakePHP 2.x = 3.x upgrade smoothly , Controller layer 1.find('list') In the original 2.x, this method implements key=value 3 by specifying the 'fields' in the option...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template