Cakephp多个输入字段

时间:2016-10-26 00:20:18

标签: cakephp

我是cakephp的新手。我有一个有5个输入的表格。我的表单应该能够保存一个用户输入或全部5个输入。我可以在用户填写所有5个输入时保存,但是,当用户只填充1或2并保存时。创建日期的空格(当前日期)将保存在数据库中。如何使它只保存表单中的用户输入而数据库中没有任何空字段。我的下面的添加功能。

public function 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'));
}

2 个答案:

答案 0 :(得分:0)

您遗失了一件小事,而且是否有名称为空,但是它具有为该特定索引设置的值。如果值为空,则应取消设置,如下所示

public function add() {

    if ($this->request->is('post')) {
        $this->Item->create();

        for ($i=0;$i<5;$i++){
            if(empty($this->request->data['Item'][$i]['name'])){
                // here we are removing the empty name index so that it does not saves the result
                unset($this->request->data['Item'][$i]);
            }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];
            }   
        }

        // also here we should check here that atleast there is one entry
        if(!empty($this->request->data['Item'])){
            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.'));
            }
        } else {
                $this->Session->setFlash(__('There is no such item. Please fill value for at least one item.'));
        }
    }   
    $itemTypes = $this->Item->ItemType->find('list',array('order' =>array('ItemType.name' => 'asc')));
    $this->set(compact('itemTypes'));
}

请尝试以上代码。

答案 1 :(得分:0)

CakePHP 2.x中,您可以按照以下方式执行操作 -

public function add(){
  if ($this->request->is('post')) {
  $this->Item->create();
  $items = $this->request->data['Item']; /*Get all items Array*/
  $items = array_filter(array_map('array_filter', $items)); /*Remove all empty array, only keep Array with user inputs*/
  if ($this->Item->saveAll($items)) {
    /*Success*/
  } else {
    /*Error*/
  }
}

}

CakePHP 3.x中,您可以按照以下方式执行操作 -

public function add() {
  if ($this->request->is('post')) {
    $items = $this->request->data['Item']; /*Get all items Array*/
    $items = array_filter(array_map('array_filter', $items)); /*Remove all empty array, only keep Array with user inputs*/
    $entities = $this->Item->newEntities($items); /*Prepare all Data*/
    if($this->Item->saveMany($entities)){ /*Save all data*/
       /*Success*/
    }else{
       /*Error*/
    }
  }
}