cakephp 中对于数据库的增删查改操作
2015-12-12 23:38
701 查看
查找:
增加:
修改:
删除:
$user_id=$this->request->session()->read('User.id'); $query = $this->Devicesdatas ->find() ->where(['user_id' => $user_id]); $this->paginate = [ 'contain' => ['Users','Devices'], 'limit' => 10 ]; $this->set('devicesdatas', $this->paginate($query)); $this->set('_serialize', ['devicesdatas']);
增加:
$devicesdata = $this->Devicesdatas->newEntity(); if ($this->request->is('post')) { $devicesdata = $this->Devicesdatas->patchEntity($devicesdata, $this->request->data); if ($this->Devicesdatas->save($devicesdata)) { $this->Flash->success(__('The devicesdata has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The devicesdata could not be saved. Please, try again.')); } } $users = $this->Devicesdatas->Users->find('list', ['limit' => 200]); $phones = $this->Devicesdatas->Phones->find('list', ['limit' => 200]); $devices = $this->Devicesdatas->Devices->find('list', ['limit' => 200]); $this->set(compact('devicesdata', 'users', 'phones', 'devices')); $this->set('_serialize', ['devicesdata']);
修改:
$query = $this->Devicesdatas ->find() ->order(['created' => 'DESC']) ->limit(1); //execute query; $results=$query->all(); foreach ($results as $result){ $result->img_url=$img_url; if($this->Devicesdatas->save($result)) echo 'success'; }
删除:
public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); $devicesdata = $this->Devicesdatas->get($id); if ($this->Devicesdatas->delete($devicesdata)) { $this->Flash->success(__('The devicesdata has been deleted.')); } else { $this->Flash->error(__('The devicesdata could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); }
相关文章推荐
- ProFTP如何热加载配置文件
- ORA-06577:output parameter not a bind variable
- [hdoj试题]A+B for Input-Output Practice (VI)
- 内容提供者ContentProvider的使用详解
- [hdoj试题]A+B for Input-Output Practice (V)
- ProFTP安装及配置
- 编译LAMP 环境之PHP
- [hdoj试题]A+B for Input-Output Practice (IV)
- PHP String字符串函数
- [hdoj试题]A+B for Input-Output Practice (III)
- PHP closure 匿名函数
- [hdoj试题]A+B for Input-Output Practice (II)
- [hdoj试题]A+B for Input-Output Practice (II)
- PHP给第三方接口POST或GET方式传输数据并得到返回值
- 使用IMP导出数据备份并简单FTP实现异地存储
- php环境搭建及设置出入站规则
- PHP语言编写的四条指南
- PHP扩展:第三个程序
- php 小计----常量名称和值的在单双引号中的搭配使用
- php运算符