I realize I'm not the first person to have a problem with saveAll(). I
have searched this group for a solution to my problem, which is - I
can't get saveAll() to save related model data. I must be doing
something wrong but I cannot spot it.
My app is a sales/inventory system. Here are the pertinent relations:
Vendor hasMany Item
Item hasMany SalesTransaction
I am trying to, in one call to saveAll(), save both an updated
Item.quantity and insert a new SalesTransaction record
My Item model code for the relations is this:
var $belongsTo = array(
'Vendor' => array('counterCache' => true)
);
var $hasMany = array(
'SalesTransaction' => array(
'className' => 'SalesTransaction',
'foreignKey' => 'item_id'
)
);
My SalesTransaction model code for the relation is this:
var $belongsTo = array(
'Item' => array('className' => 'Item',
'foreignKey' => 'item_id',
'fields' => array('id', 'quantity')
)
);
For brevity, I am leaving out some other models that don't pertain to
this particular problem.
In my controller code, I construct the array ($items) to pass to
saveAll().
debug($items); output looks like this:
Array
(
[0] => Array
(
[id] => 1250
[price] => 14.29
[quantity] => 9
[Vendor] => Array
(
)
[SalesTransaction] => Array
(
[0] => Array
(
[sale_id] => 1404
[type_id] => 1
[item_id] => 1250
[old_qty] => 10
[new_qty] => 9
)
)
)
[1] => Array
(
[id] => 1251
[price] => 28.57
[quantity] => 9
[Vendor] => Array
(
)
[SalesTransaction] => Array
(
[0] => Array
(
[sale_id] => 1404
[type_id] => 1
[item_id] => 1251
[old_qty] => 10
[new_qty] => 9
)
)
)
[2] => Array
(
[id] => 1252
[price] => 42.86
[quantity] => 9
[Vendor] => Array
(
)
[SalesTransaction] => Array
(
[0] => Array
(
[sale_id] => 1404
[type_id] => 1
[item_id] => 1252
[old_qty] => 10
[new_qty] => 9
)
)
)
)
The code that does the saveAll() is:
$this->Item->saveAll($items, array('fieldList'=>array('quantity')))
The debug output looks like this:
START TRANSACTION 0 0
19 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`id` = 1250 1 1 1
20 UPDATE `items` SET `quantity` = 9, `modified` = '2011-01-17
15:00:46' WHERE `items`.`id` = 1250 1 1
21 SELECT `Item`.`vendor_id` FROM `items` AS `Item` WHERE `Item`.`id`
= 1250 LIMIT 1 1 1 0
22 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`vendor_id` = 276 1 1 2
23 UPDATE `vendors` AS `Vendor` SET `Vendor`.`item_count` = 3 WHERE
`Vendor`.`id` = 276 0 1
24 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`id` = 1251 1 1 1
25 UPDATE `items` SET `quantity` = 9, `modified` = '2011-01-17
15:00:46' WHERE `items`.`id` = 1251 1 1
26 SELECT `Item`.`vendor_id` FROM `items` AS `Item` WHERE `Item`.`id`
= 1251 LIMIT 1 1 1 1
27 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`vendor_id` = 276 1 1 2
28 UPDATE `vendors` AS `Vendor` SET `Vendor`.`item_count` = 3 WHERE
`Vendor`.`id` = 276 0 1
29 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`id` = 1252 1 1 1
30 UPDATE `items` SET `quantity` = 9, `modified` = '2011-01-17
15:00:46' WHERE `items`.`id` = 1252 1 1
31 SELECT `Item`.`vendor_id` FROM `items` AS `Item` WHERE `Item`.`id`
= 1252 LIMIT 1 1 1 1
32 SELECT COUNT(*) AS `count` FROM `items` AS `Item` WHERE
`Item`.`vendor_id` = 276 1 1 2
33 UPDATE `vendors` AS `Vendor` SET `Vendor`.`item_count` = 3 WHERE
`Vendor`.`id` = 276 0 1
34 COMMIT
BTW, I have an afterSave() in Items that updates the item_count in
Vendors.
It updates the 3 Item.quantity values but does not insert new
SalesTransaction records.
I would appreciate any help you can give.
Ken
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en