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中加入關聯鍵???
此升級手順會持續更新中~~~~~~
http://www.bkjia.com/PHPjc/984441.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/984441.htmlTechArticleCakePHP 2.x 升級3.0,cakephp2.x升級3.0 CakePHP 2.x = 3.x 升級手順 一、Controller層 1.find('list') 原2.x中該方法通過指定option中的'fields'來實現key=value 3...