0

我对 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 ) ) )
4

1 回答 1

1

您正在告诉模型 ArchiveOrder 保存订单条目。

您所要做的就是更改 ArchiveOrder 的键 Order

$archived_data = array();
for ($data as $order => $o) {
  $archived_data[]['ArchivedOrder'] = $o['Order'];  
}

然后将 $archived_data 保存在 $data 中。

于 2013-05-29T02:13:22.767 回答