2013-05-27 75 views
0

我試圖從一個表複製多個行到另一個,但它只保存我選擇的最後一個條目。當我打印所有顯示的數據時。我正在複製的表沒有設置auto_increment,這是我爲什麼猜測,所以現在我感到困惑。CakePHP沒有正確保存條目

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

每CakePHP的書here

當調用保存在一個循環中,不要忘了調用create()。

也可能不是一個好主意,在foreach循環中執行loadModel

// load the model 

// foreach loop 

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

    //... 
+0

沒有工作。我最終只是使用$ this-> ArchivedOrder-> query(「insert into ...」)來使它工作。 – user1443519

+0

@ user1443519 - 不要使用「insert into ...」學會先使用Cake正確的方法。 – Dave

0

有多種問題與代碼

  • 不要使用查詢()... SQL注入未來,你的代碼是不安全的,你失去的數據庫抽象。使用find()。
  • 不是一個問題,但是... print_r的 - 有調試()由CakePHP的,它比普通的print_r
  • 不要使用保存()好很多,用白水(),讀that page。無需將數據保存到一個循環,代碼的
  • 90%要到模型不是在一個控制器
+0

你能指定模型中應該做什麼嗎?僅僅說「90%」並不是很有幫助。 – user1443519

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