简体   繁体   English

CakePHP无法将数据正确保存在表中

[英]CakePHP not saving data in table properly

I'm still relatively new to CakePHP so I'm not entirely sure if what I'm doing is considered bad practice. 我还是CakePHP的新手,所以我不确定我在做什么是否被认为是不好的做法。 I'm trying to transfer data from one table to another for archiving purposes. 我试图将数据从一个表转移到另一个表以进行存档。 The tables are labeled identically, although the archived_orders table doesn't have an auto_incrementing primary key because not every order will be archived. 这些表的标签相同,尽管archived_orders表没有auto_incrementing主键,因为并非所有订单都会被存档。 When I try to save the data the number of rows appears properly, but every entry is null. 当我尝试保存数据时,行数会正确显示,但是每个条目都为空。

Here are the exported tables 这是导出的表

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;

Here is the code that I'm using to transfer. 这是我用来传输的代码。

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

Here is what the print statement returns. 这是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 ) ) )

Two rows of "null" appear in my archived_orders table aside from the times which are properly set. 除了正确设置的时间外,我的archived_orders表中还显示两行“空”。 My ArchivedOrder model is blank because the only thing I use is a table for storage. 我的ArchivedOrder模型为空,因为我唯一使用的是用于存储的表。 There are no actions to complete, it's just a table that I use. 没有要完成的动作,这只是我使用的表格。 Is this causing issues? 这会引起问题吗?

Edit - this is according to davidmh's 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);

Only the second item of the array is saved in the table. 表中仅保存数组的第二项。 This is what the print statement returns 这是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 ) ) )

You are telling the model ArchiveOrder to save a Order entry. 您正在告诉模型ArchiveOrder保存一个Order条目。

All you have to do is change the key Order for ArchiveOrder 您所要做的就是将关键Order更改为ArchiveOrder

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

And then save $archived_data insead of $data. 然后将$ archived_data保存为$ data。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM