mscdex | 1 Feb 2009 10:05
Picon
Gravatar

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
(Continue reading)

mscdex | 1 Feb 2009 17:00
Picon
Gravatar

Re: Batch adding records using Tree Behavior


On Feb 1, 4:05 am, mscdex <msc...@...> wrote:
> 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
>     )
>
(Continue reading)


Gmane