Cakephp 多个输入字段

Posted

技术标签:

【中文标题】Cakephp 多个输入字段【英文标题】:Cakephp multiple input fields 【发布时间】: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的调试结果;或者只是让您在代码的其他部分保存功能! 【参考方案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*/
    
  

【讨论】:

以上是关于Cakephp 多个输入字段的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP:当有多个关联记录时如何指定返回字段

将输入字段添加到所有表单中 - cakephp

CakePHP:向表单添加字段(动态)

CakePHP 3:上传多个文件并将文件名保存在关联模型中?

CakePHP 3.x:将一个输入保存到多个表中

cakePHP 保存到多个模型