Sunday, February 1, 2009

Batch adding records using Tree Behavior

I have a model that contains a hasMany relationship with a model that
"actsAs" a Tree. This Tree model has a hasMany 'child' relationship as
well as a belongsTo 'parent' relationship to the same model. This
setup works just fine when the user is interactively adding children
nodes and such. However, I also have a function that gathers data from
a flat file and inserts new records based on that data using saveAll.

My problem is I cannot find a quick and easy way to automatically
insert children at the same time. If I have a multidimensional array
('TodoChild' in the example below), is there a way to get the Tree
Behavior to use it to create the children and automatically assign
them to the proper parent?

The only way I thought to do it was by somehow using a recursive
method and doing a save on the parent first, then set the parent_id
for each direct child below it and saving each of those (then repeat
recursively). But that approach just seemed a bit much just to simply
add children.

My hasMany and belongsTo relationships for the model with Tree
Behavior only have the foreignKey set to "parent_id." Here is an
example data array with the same structure I am working with:

[User] => Array
(
[name] => John
)

[Todo] => Array
(
[0] => Array
(
[type] => single
[note] => bread, milk, eggs
)

[1] => Array
(
[type] => multiple
[TodoChild] => Array
(
[0] => Array
(
[type] => single
[note] => cereal
)

[1] => Array
(
[type] => single
[note] => dessert
)

)

)

)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: