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.
Looking through documentation if stumbled upon this.
Have you given a try to fromArray
instead of hydrate
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With