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 t开发者_开发知识库o 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`
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'
)
)
精彩评论