CakePHP中的批量保存及批量更新
来源:互联网 发布:最新软件代理 编辑:程序博客网 时间:2024/06/07 04:38
在CakePHP中,批量保存及批量更新主要用到 Cake\ORM\Table::newEntities(array $data, array $options[])
、Cake\ORM\Table::patchEntities(array|Traversable $entities , array $data , array $options[])
及 Cake\ORM\Table::saveMany(array|Cake\ORM\ResultSet $entities , array|ArrayAccess $options[])
等方法;
批量保存
假如有一张 articles
数据表,其中有 id
title
content
published
等字段,那么在ArticlesController的方法中进行批量保存,代码大致如下:
//数组数据$data = [ [ 'title' => 'First Article', 'content' => 'This is the first article.', 'published' => 1 ], [ 'title' => 'Second Article', 'content' => 'This is the second article.', 'published' => 1 ], ...];//批量保存$entities = $this->Articles->newEntities($data);$rows = $this->Articles->saveMany($entities); //返回批量保存的条数
批量更新
假如要把所有title
中包含Article
的文章下线('published' => 0
),则在ArticlesController的方法中:
//查询需要更新的数据$articles = $this->Articles->find('all', [ 'conditions' => [ 'title LIKE' => "%Article%" ]]);//设置更新后的值foreach($articles as $article) { $data[] = array( 'id' => $article->id, //必须,用主键id进行识别 'published' => 0 );}//批量更新$articles = $this->Articles->patchEntities($articles, $data);$rows = $this->Articles->saveMany($articles); //返回批量更新的条数
批量更新还可以使用如下两种方式:
$this->Articles->updateAll( ['published' => 0], //需要更新的字段值 ['title LIKE' => '%Article%'] //查询条件);
或
$this->Articles->query() ->update() ->set(['published' => 0]) ->where(['title LIKE' => '%Article%']) ->execute();
阅读全文
1 0
- CakePHP中的批量保存及批量更新
- CakePHP中的级联删除及批量删除
- mybatis批量写入及批量更新
- EAS BOS批量保存或更新数据
- 批量更新
- 批量更新
- 批量更新
- 批量更新
- 批量更新
- 如何在cakephp或者php中用jquery实现批量修改和保存?
- 批量保存EasyUI-datagrid中的数据
- 批量保存,批量插入数据
- C# 数据批量插入及更新
- Oracle中的Merge函数(批量更新/删除)
- SQL中的事务以及批量更新
- SQL中的事务以及批量更新
- MySQL批量更新不同表中的数据
- mysql 批量更新临时表中的数据
- SpringMvc @InitBinder 表单多对象精准绑定接收
- Docker的四种网络模式
- SetupAPI简介与设备管理
- centos学习:理解main函数的两个参数
- 《200行Python代码实现2048》分析
- CakePHP中的批量保存及批量更新
- 移动端屏幕适配的解决
- Thinking in java 笔记(更新中)
- Java Lambda(语言篇——lambda,方法引用,目标类型,默认方法,函数接口,变量捕获)
- 第八课、数组
- 014--利用模型的didSet属性给UI赋值
- 【电影】英国男演员杠把子
- hdu 5873 Football Games 思维
- 总线接口