0
對於CakePHP我還是比較新的,所以我不完全確定我在做什麼被認爲是不好的做法。我試圖將數據從一個表格傳輸到另一個表格用於歸檔目的。雖然archived_orders表沒有auto_incrementing主鍵,但表格的標籤是相同的,因爲並不是每個訂單都將被存檔。當我嘗試保存數據時,行數正確顯示,但每個條目都爲空。CakePHP沒有正確保存表格中的數據
下面是導出的表
CREATE TABLE `orders` (
`orderid` int(10) NOT NULL auto_increment,
`id` int(10) default NULL,
`order_status` varchar(12) NOT NULL default 'Not Filled',
`email` varchar(50) NOT NULL,
`total` varchar(50) NOT NULL,
`fullName` varchar(60) NOT NULL,
`address` varchar(50) NOT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(2) NOT NULL,
`zip` int(5) NOT NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`orderid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
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(2) default NULL,
`zip` int(5) default NULL,
`created` datetime default NULL,
`modified` datetime default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
下面是我使用傳輸的代碼。
//OrdersController.php
$data = $this->Order->find('all',array(
'conditions' => array('order_status'=> "filled",
'id' => AuthComponent::user('id'))
));
print_r($data); //this displays the orders so I know it's finding them properly
$this->loadModel('ArchivedOrder');
$this->ArchivedOrder->SaveAll($data);
這是打印語句返回的內容。
Array ([0] => Array ([Order] => Array ([orderid] => 1 [id] => 4 [order_status] => filled [email] => [email protected] [total] => 1.00 [fullName] => Test1 [address] => 1234 Test [city] => Test [state] => NY [zip] => 12345 [created] => 2013-05-28 06:30:00 [modified] => 2013-05-28 06:30:00))
[1] => Array ([Order] => Array ([orderid] => 2 [id] => 4 [order_status] => filled [email] => [email protected] [total] => 2.00 [fullName] => Test2 [address] => 1 Test2 [city] => Test2 [state] => PA [zip] => 12345 [created] => 2013-05-28 06:30:00 [modified] => 2013-05-28 06:30:00)))
除了正確設置的時間,我的archived_orders表中還出現兩行「null」。我的ArchivedOrder模型是空白的,因爲我唯一使用的是存儲表。沒有任何行動要完成,這只是我使用的一張桌子。這是造成問題嗎?
編輯 - 這是根據davidmh的suggestsions
$this->loadModel('ArchivedOrder');
$archived_data = array();
foreach($data as $order => $o) {
$archived_data[]['ArchivedOrder'] = $o['Order'];
}
print_r($archived_data);
$this->ArchivedOrder->SaveAll($archived_data);
只有數組的第二個項目被保存在表中。這是打印聲明返回的內容
Array ([0] => Array ([ArchivedOrder] => Array ([orderid] => 1 [id] => 4 [order_status] => filled [email] => [email protected] [total] => 1.00 [fullName] => Test1 [address] => 1234 Test [city] => Test [state] => NY [zip] => 12345 [created] => 2013-05-28 06:30:00 [modified] => 2013-05-28 06:30:00))
[1] => Array ([ArchivedOrder] => Array ([orderid] => 2 [id] => 4 [order_status] => filled [email] => [email protected] [total] => 2.00 [fullName] => Test2 [address] => 1 Test2 [city] => Test2 [state] => PA [zip] => 12345 [created] => 2013-05-28 06:30:00 [modified] => 2013-05-28 06:30:00)))
它轉換數組,但它只保存最後一個條目。我更新了我的帖子,上面顯示 – user1443519
我想我明白了。 CakePHP不喜歡我將用戶稱爲id而不是userid,並且它只允許每個表有一個唯一的'id',所以它不會保存同一用戶的多個訂單。 – user1443519
您可以隨時通過$ this-> primaryKey ='fieldname''更改模型中的主鍵。 – nahri