8

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • @j0k why shouldn't I thank for answer? – shadyyx Sep 19 '12 at 15:16
  • 3
    Site policy, no sign offs, thanks, that sort of thing in the actual question - as it's not part of the question. Fine in comments. – Orbling Sep 19 '12 at 15:26
  • 2
    See [this thread](http://meta.stackexchange.com/q/2950/182741). – j0k Sep 19 '12 at 15:31
  • Have you tried `HYDRATE_ON_DEMAND`? – Ja͢ck Sep 26 '12 at 02:31
  • @Jack, HYDRATE_ON_DEMAND was the only thing I wanted to prevent, because of lazy-loading. Anyway, hydrate on demand hydrates objects but only when they are needed. My question was how to transer model object array into a doctrine record object with all the relations. – shadyyx Sep 26 '12 at 08:41

2 Answers2

2

Looking through documentation if stumbled upon this.

Have you given a try to fromArray instead of hydrate?

jaudette
  • 2,305
  • 1
  • 20
  • 20
  • I havent't tried that `fromArray()`. However I'm not working on that project few months now, I had to use lazy-loading wich I was trying to avoid... If I were doing some modifications on that project again, I would give it a try. Will accept Your answer for now as it looks promising! – shadyyx Nov 26 '12 at 09:21
0

Trying to answer this to the best of my Doctrine knowledge, which isn't much because I never dealt directly with hydration.

The problem with using an array as the hydration method is that you basically lose all the meta data that was attached to the original query, most importantly the table names.

A solution to this could be to write your own hydration method that converts the array back into objects.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309