I'm still relatively new to CakePHP so I'm not entirely sure if what I'm doing is considered bad practice. 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. 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.
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. My ArchivedOrder model is blank because the only thing I use is a table for storage. 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
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 ) ) )