Cakephp使用foreach的多个记录

时间:2014-04-01 12:38:52

标签: cakephp

我正在尝试使用cakephp插入多个记录。我的控制器代码如下:

if (!empty($this->request->data)) {
            foreach($this->request->data['Deal']['deal_date'] as $key => $data)
            {
                $this->Deal->create();
                $this->Deal->id = $id;
                $this->request->data['Deal']['deal_date'] = $data;
                $this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
                if ($this->Deal->save($data)) {
                   $this->Session->setFlash(__('The deal has been saved'));
                   //$this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
                }

            }

        }

它没有正确地将数据插入数据库。我在所有输入字段中使用了数组。任何想法?

3 个答案:

答案 0 :(得分:2)

使用saveAll or saveMany

if (!empty($this->request->data)) {
    $records = array();
    foreach($this->request->data['Deal']['deal_date'] as $key => $data) {
        $record = $this->request->data;
        $record['Deal']['id'] = $id; // are you sure you need this ?
        $record['Deal']['deal_date'] = $data;
        $record['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
        $records[] = $record;
    }

    if ($this->Deal->saveMany($records)) {
        $this->Session->setFlash(__('The deal has been saved'));
    } else {
        $this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
    }

}

答案 1 :(得分:0)

//试试这个

if (!empty($this->request->data)) {
            $myData = array();
        foreach($this->request->data['Deal']['deal_date'] as $key => $data)
        {
            $this->Deal->create();
            $myData['Deal']['deal_date'] = $data;
            $myData['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
            if ($this->Deal->save($myData)) {
               $this->Session->setFlash(__('The deal has been saved'));
               //$this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
            }
            unset($myData);
        }

    }

答案 2 :(得分:0)

要插入多行,请使用cornelb提及的savemany或saveall, 但尝试通过打印sql log来调试代码无法正常工作的原因

if (!empty($this->request->data)) {
            foreach($this->request->data['Deal']['deal_date'] as $key => $data)
            {
                $this->Deal->create();
                $this->Deal->id = $id;
                $this->request->data['Deal']['deal_date'] = $data;
                $this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
                if ($this->Deal->save($data)) {
                   $log=$this->Deal->getDataSource()->getLog(false, false);
                   echo "<pre>";print_r($log);exit;
                   $this->Session->setFlash(__('The deal has been saved'));
                   //$this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
                }

            }

        }