假设有表books ,其中分别有字段id,book_name,type_id,type_name,year,status,create_time
1. 添加数据
$this->loadModal('Book'); $data['book_name'] = '雨季不再来'; $data['type_id'] = 4; $data['type_name'] = '小说'; $data['year'] '2014'; $data['status'] = 1; $data['create_time'] = date('Y-m-d H:i:s'); $result = $this->Book->save($data); //pr($result); --->结果为 Array ( [BookMedal] => Array ( [book_name] => 雨季不再来 [type_id] => 4 [type_name] => 小说 [year] => 2014 [status] => 1 [create_time] => 2014-03-13 14:31:34 ) ) //判断是否保存成功 if($result){ echo 'ok'; }else{ echo 'wrong'; }?
?//多条保存
$data[0]['book_name'] = '雨季不再来'; $data[0]['type_id'] = 4; $data[0]['type_name'] = '小说'; $data[0]['year'] '2014'; $data[0]['status'] = 1; $data[0]['create_time'] = date('Y-m-d H:i:s'); $data[1]['book_name'] = '数学高考联系'; $data[1]['type_id'] = 3; $data[1]['type_name'] = '数学'; $data[1]['year'] '2014'; $data[1]['status'] = 1; $data[1]['create_time'] = date('Y-m-d H:i:s'); $result = $this->Book->saveAll($data);?2. 删除数据
$this->loadModal('Book');
//单条删除
$result = $this->Book->delete(4); ? //删除id为4的数据
//多条删除
$result = $this->Book->deleteAll(array('status'=>0));//删除状态为冻结的数据
?
3. 修改数据
1)单条修改,如修改id为12的状态为冻结
$this->loadModal('Book'); $result = $this->Book->updateAll(array('status' => 0), array('id' => 12));?
2)多条修改,修改名称带‘语文’的状态为冻结
$result = $this->Book->updateAll(array('status' => 0), array('book_name like' => '%语文%')); //修改id为1,2,3,4的类别为‘数学’ $result = $this->Book->updateAll(array('type_name' => '数学','type_id'=>3), array('id' => array( 1, 2, 3, 4 )));
4. 查询数据
$this->loadModal('Book'); //查询总数 $result = $this->Book->find('count', array('conditions'=>array('status'=>1), //条件是status=1 'fileds'=>array('book_name','type_name','create_time'), //查询的字段 'order'=>array('id'=>'DESC'), //按id倒序排列 'limit'=>"{$start},{$this->perpage}" //分页查询 ) );?