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

Source: Internet
Author: User

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

CakePHP 2.x => 3.x upgrade

I. Controller Layer

1. find ('LIST ')
In the original 2. x, This method uses 'fields' in the specified option to implement key => value
3. x is replaced by 'keyfield' and 'valuefield 'in option, and the return value must be converted using toArray ().

2. find ('first '), find ('Count ')
3. x has completely discarded the two parameters and replaced them with find ()-> first () and find ()-> count ().

3. When you find a joined table, you must add the 'containing' attribute. By default, the table is not associated with Cake 3.x.

4. findById
**? No documentation found? **
In 2.x, The findById format is ($ id, array $ fields)
3. There may be only one parameter ($ id) in array x. You need to use the select (array $ fields) method to filter data.

5. Component-> initialize ()
2. x get the Controller that calls this Component through initialize (Controller $ Controller)
3. x changes the initialize parameter to initialize (array $ config), which is obtained through $ controller = $ this-> _ registry-> getController ();

6. SessionComponent
3. x discards the Component and replaces it with $ this-> request-> session ().

Ii. Model Layer

1. Associations
2. x implement association by defining attributes
3. x needs to be implemented by calling $ this-> belonsto, $ this-> hasMany, $ this-> belongstoations, $ this-> addAssociations and other methods in the initialize method in the Table.

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

3. virtualFields
3. x discards this attribute. To use this attribute, You need to append the method to Entity.
For example, you need table A to define the virtual field full_name.
First, add protected function _ getFullName () to the Entity of Table ()
Add full_name to $ _ accessible.
After A-> find is used, the value can only be obtained through object-> property, and cannot be obtained through array.

4. field ()
**? No documentation found? **
3. x may be discarded. This method can be replaced by the get () method.

5. ClassRegistry: init ()
3. x Replace the method with TableRegistry: get ().

6. Mysql alias settings
2. x only needs to add as to the fields attribute, for example, 'fields' => array ('Id as table_id ')
3. If an alias is defined, add a key such as 'fields' => ['table _ id' => 'id'].

3. View Layer

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

1. $ this-> Html-> url
Replace $ this-> Url-> build in 3.x

Iv. Bug

1. Associations
If you want to specify fields for table join queries in hasales, you must add the? In fields ???

This upgrade will continue to be updated ~~~~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.