Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 1.2 hydrate method - array to model object

I have a little problem with Doctrine model hydrate() method. I use this method to hydrate an object of conrete model from a given array like so:

$model = new Doctrine\Model\Model;
$model->hydrate($model_array);

Everything works perfect when hydrating simple objects withou nested sub-models. Now the problem is I need to hydrate (using this method) an object that has nested objects (and some of them have also a nested objects).

If I were using HYDRATE_RECORD that would be fine but all the records from query would be returned as objects which means more memory consumption. Therefore I am using HYDRATE_ARRAY and on demand hydrate that concrete array to an object.

Let's suppose I have a model A that has nested models AB, AC (by one to many), AD and AC has another nested model ACE. After print_r of the A array we could see this structure:

A Array (
    ...
    ab Array ( ... )
    ac Array (
        AC Array (
            ...
            ace Array ( ... )
        )
        AC Array (
            ...
            ace Array ( ... )
        )
        ...
    )
    ad Array ( ... )
)

Normally after using hydrate I would assume that this would be my object:

A Object {
    ...
    ab Object { ... }
    ac Array (
        AC Object {
            ...
            ace Object { ... }
        }
        AC Object {
            ...
            ace Object { ... }
        }
        ...
    )
    ad Object { ... }
}

But instead of this I get this structure:

A Object {
    ...
    ab Array ( ... )
    ac Array (
        AC Array (
            ...
            ace Array ( ... )
        )
        AC Array (
            ...
            ace Array ( ... )
        )
        ...
    )
    ad Array ( ... )
}

So only the main model got converted to an object. Do You know about a way how to get all the nested model arrays got converted to an objects like the supposed result?

And no, I cannot use HYDRATE_RECORD when querying the DB.

like image 312
shadyyx Avatar asked Sep 19 '12 14:09

shadyyx


1 Answers

Looking through documentation if stumbled upon this.

Have you given a try to fromArray instead of hydrate?

like image 89
jaudette Avatar answered Oct 01 '22 02:10

jaudette