蛋糕php在循环中插入查询

时间:2014-02-03 05:30:09

标签: php cakephp

Hello每个人我都在控制器中有一个功能,即我在提交表格的结账处。 但是我想在下面的循环中运行插入查询$ this->会话 - >读取('购物车')值是3但是我的代码在表中插入一条记录如果我在内部循环警报它警报3次怎么能我运行插入查询3次请任何人帮助我。 提前致谢。


public function checkout() {
        $this->loadModel("Checkout");
        Configure::read();
        $aorderno = $this->Checkout->find('all', array('order' => array('Checkout.id' => 'DESC'),'limit' => 1));    
        $this->set('aorderno',$aorderno);
           foreach($aorderno as $order)
           {
           $myorderno=$order['Checkout']['orderno']+1;
           }
        $firstname = $this->request->data['checkout']['firstname'];
        $this->set('firstname',$firstname);
        $lastname = $this->request->data['checkout']['lastname'];
        $this->set('lastname',$lastname);
if(count($this->Session->read('Cart'))>0)
             {
                    foreach($this->Session->read('Cart') as $value)
                    {
                    $sku=$value['Product']['sku'];
                    $this->Checkout->save(array('orderno' => $myorderno,'firstname' => $firstname,'lastname' => $lastname'sku' => $sku));
                    }
             }
}

2 个答案:

答案 0 :(得分:1)

我想你的$ this-> Checkout-> saveQuery函数。

您正在使用类似$ this-> save();

的内容

在$ this-> save()之前,请调用$ this-> create();

希望它有所帮助。

答案 1 :(得分:0)

您可以使用saveMany

    foreach(.......){
        $arr[]= array('orderno' => $myorderno,'firstname' => $firstname,'lastname' => $lastname'sku' => $sku);
    }

   $this->Checkout->saveMany($arr, array('deep' => true));