drxd54816 2011-05-20 12:56
浏览 33
已采纳

使用CakePHP将数据保存到新表

I have an order database and wish to save the orders in a new database once the order is completed. My Order model has a table with id, customer, partnumber etc. and my OldOrder has a copy of the Order table.

Once an order is completed, my plan was to save Order data to the OldOrder model and then delete the order from the Order model. This way I can have a small current orders database and let the old order database expand.

I'm getting confused at the best way to do this, my attempt was to create an action in my Order model such as this (this is just designed to copy the data to a the OldOrder model):

function save_order() {
    if(!empty($this->data)){
        $id = $this->data['Order']['id'];
        $this->data = $this->Order->find('first', array('conditions' => array('Order.id' => $id)));
        $this->loadModel('OldOrder');
        $this->OldOrder->save($this->data);
    }
}

However this is not working. I'm not getting any error messages but nothing is saved to the OldOrder table. Any information why this is happening or a better way to achieve what I want to do would be greatly appreciated!

EDIT:

This is the SQL dump from the action:

1 SHOW FULL COLUMNS FROM `orders`
2 SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= 'latin1_swedish_ci';       1   1   4
3 SHOW FULL COLUMNS FROM `users`
4 SELECT `Order`.`customer`, `Order`.`length`, `Order`.`height`, `Order`.`date`, `Order`.`quantity`, `Order`.`order_number`, `Order`.`date_created`, `Order`.`date_dispatched`, `Order`.`id` FROM `orders` AS `Order` WHERE `Order`.`id` = 17 
5 SHOW FULL COLUMNS FROM `old_orders`
  • 写回答

1条回答 默认 最新

  • douduan4116 2011-05-20 13:28
    关注

    You need to pass the Order data to the OldOrder model's save method like this:

    $this->OldOrder->save(array('OldOrder' => $this->data['Order']));
    

    Or you can also do it like this:

    $this->OldOrder->set($this->data['Order']);
    $this->OldOrder->save();
    

    From the CakePHP documentation:

    CakePHP makes saving model data a snap. Data ready to be saved should be passed to the model’s save() method using the following basic format:

     Array
     (
         [ModelName] => Array
             (
                 [fieldname1] => 'value'
                 [fieldname2] => 'value'
             )
     )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?