我仍然是CakePHP的新手,所以我不能完全确定我所做的事情是否被认为是不好的做法。我正在尝试将数据从一个表传输到另一个表以进行存档。这些表标记相同,但archived_orders表没有auto_incrementing主键,因为并非每个订单都将被归档。当我尝试保存数据时,行数正确显示,但每个条目都为空。
以下是导出的表格
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);
这是print语句返回的内容。
Array ( [0] => Array ( [Order] => Array ( [orderid] => 1 [id] => 4 [order_status] => filled [email] => test@test.com [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] => test2@test.com [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的建议
$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);
只有数组的第二项保存在表格中。这是print语句返回的内容
Array ( [0] => Array ( [ArchivedOrder] => Array ( [orderid] => 1 [id] => 4 [order_status] => filled [email] => test@test.com [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] => test2@test.com [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 ) ) )
答案 0 :(得分:1)
您告诉模型ArchiveOrder保存订单条目。
您所要做的就是更改ArchiveOrder的订单
$archived_data = array();
for ($data as $order => $o) {
$archived_data[]['ArchivedOrder'] = $o['Order'];
}
然后保存$ archived_data insead of $ data。