PHP头条
热点:

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中加入关联键???

此升级手顺会持续更新中~~~~~~

www.phpzy.comtrue/php/35787.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.x通过option中的'keyField'和'valueField'来替换,返回值...

相关文章

    暂无相关文章

PHP之友评论

今天推荐