【发布时间】:2016-10-26 00:20:17
【问题描述】:
我是 cakephp 的新手。我得到了一个有 5 个输入的表格。我的表单应该能够保存一个用户输入或所有 5 个输入。当用户填写所有 5 个输入时,我可以保存,但是,当用户仅填写 1 个或 2 个并保存它时。创建日期(当前日期)的空格保存在数据库中。我怎样才能让它只保存表单中的用户输入,而数据库中没有任何空字段。下面是我的 Add 函数。
公共函数添加(){
if ($this->request->is('post')) {
$this->Item->create();
for ($i=0;$i<5;$i++){
if(empty($this->request->data['Item'][$i]['name'])){
}else{
$name = $this->request->data['Item'][$i]['name'];
$explode_name = explode(":",$name);
$this->request->data['Item'][$i]['name'] = $explode_name[0];
$this->request->data['Item'][$i]['hrid'] = $explode_name[1];
}
}
if ($this->Item->saveAll($this->request->data['Item'])) {
$this->Session->setFlash(__('The Item has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The item could not be saved. Please, try again.'));
}
}
$itemTypes = $this->Item->ItemType->find('list',array('order' =>array('ItemType.name' => 'asc')));
$this->set(compact('itemTypes'));
}
【问题讨论】:
-
能否显示$this->request->data的调试结果;或者只是让您在代码的其他部分保存功能!
标签: cakephp