【问题标题】:Cakephp multiple input fieldsCakephp 多个输入字段
【发布时间】: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


【解决方案1】:

您缺少一件小事,那就是名称是否为空,但它为该特定索引设置了一个值。如果值为空,您应该取消设置,如下所示

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

请试试上面的代码。

【讨论】:

    【解决方案2】:

    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*/
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多