douyuan4697 2013-05-29 02:04
浏览 43
已采纳

CakePHP没有正确保存表中的数据

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 ) ) )
  • 写回答

1条回答 默认 最新

  • dongsuiwo0279 2013-05-29 02:13
    关注

    You are telling the model ArchiveOrder to save a Order entry.

    All you have to do is change the key Order for ArchiveOrder

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

    And then save $archived_data insead of $data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度