CakePHP2.x upgrade 3.0

Source: Internet
Author: User
: This article describes how to upgrade CakePHP2.x to 3.0. For more information about PHP tutorials, see. 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 ~~~~~~

The above section describes how to upgrade CakePHP 2.x to 3.0, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.