CakePHP没有正确保存条目

时间:2013-05-27 21:26:01

标签: php mysql cakephp

我正在尝试将多个行从一个表复制到另一个表,但它只保存我选择的最后一个条目。当我打印数据数组时,一切都会显示出来。我正在复制的表没有设置auto_increment,这就是我猜的原因,所以现在我很困惑。

foreach($this->request->data['Order']['archive_value'] as $orderid){
    $data = $this->Order->query("select * from orders where orderid = '$orderid'");
    $newdata = array(
        'ArchivedOrder' => array(
                'orderid' => $data[0]['orders']['orderid'],
                'id' => $data[0]['orders']['id'],
                'order_status' => $data[0]['orders']['order_status'],
                'email' => $data[0]['orders']['email'],
                'total' => $data[0]['orders']['total'],
                'fullName' => $data[0]['orders']['fullName'],
                'address' => $data[0]['orders']['address'],
                'city' => $data[0]['orders']['city'],
                'state' => $data[0]['orders']['state'],
                'zip' => $data[0]['orders']['zip'],
                'created' => $data[0]['orders']['created'],
                'modified' => $data[0]['orders']['modified']  
        ));
        print_r($newdata); //this shows every entry that I look up, but it's not saving them properly
        $this->loadModel('ArchivedOrder');
        $this->ArchivedOrder->save($newdata);
        ///$this->Order->query("delete from orders where orderid = '$orderid'");
}

当我从PHPMyAdmin转储表结构时,这就是显示

的内容
CREATE TABLE `archived_orders` (
  `orderid` int(10) default NULL,
  `id` int(10) default NULL,
  `order_status` varchar(20) default NULL,
  `email` varchar(50) default NULL,
  `total` varchar(50) default NULL,
  `fullName` varchar(60) default NULL,
  `address` varchar(50) default NULL,
  `city` varchar(50) default NULL,
  `state` varchar(50) default NULL,
  `zip` varchar(15) default NULL,
  `created` datetime default NULL,
  `modified` datetime default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

3 个答案:

答案 0 :(得分:3)

根据CakePHP书here

  

在循环中调用save时,不要忘记调用create()。

在foreach循环中执行loadModel也可能不是一个好主意。

// load the model

// foreach loop

    // build the data
    $this->ArchivedOrder->create();
    $this->ArchivedOrder->save($newdata);

    //...

答案 1 :(得分:0)

该代码存在多个问题

  • 不要使用query()... SQL注入,你的代码是不安全的,你丢失了数据库抽象。使用find()。
  • 不是问题,但是... print_r - CakePHP有debug(),比普通的print_r好多了
  • 不要使用save(),请使用saveAll(),阅读that page。无需将数据保存在循环中
  • 90%的代码应该进入不在控制器中的模型

答案 2 :(得分:0)

在OrdersController

下授予你
$newdata = array();
foreach($this->request->data['Order']['archive_value'] as $orderid){
    $data = $this->Order->find('all',array('conditions'=>array('orderid'=>$orderid)));
    if(count($data) > 0){
    $newdata[] = array(
        'ArchivedOrder' => array(
                'orderid' => $data[0]['Order']['orderid'],
                'id' => $data[0]['Order']['id'],
                'order_status' => $data[0]['Order']['order_status'],
                'email' => $data[0]['Order']['email'],
                'total' => $data[0]['Order']['total'],
                'fullName' => $data[0]['Order']['fullName'],
                'address' => $data[0]['Order']['address'],
                'city' => $data[0]['Order']['city'],
                'state' => $data[0]['Order']['state'],
                'zip' => $data[0]['Order']['zip'],
                'created' => $data[0]['Order']['created'],
                'modified' => $data[0]['Order']['modified']  
        ));
     }


}
if(count($newdata) > 0){
      $this->loadModel('ArchivedOrder');
      print_r($newdata); //this shows every entry that I look up, but it's not saving them properly
      foreach($newdata as $order):
         $this->ArchivedOrder->id = $order['ArchivedOrder']['id'];
          if($this->ArchivedOrder->exists()){
               echo 'Entered Already';
          }else{
               $this->ArchivedOrder->create();
               if($this->ArchivedOrder->save($order)){

               }else{echo 'error'; return false;}
          }
      endforeach;
}
unset($newdata);