2

I have this basic model:

enter image description here

When I fetch an entry from the book table and dump the output:

// no other Doctrine queries were made before this one:
$book = $em->getRepository('Entities\Book')->find(1);
var_dump($book);

I get the Book entity, but also, a proxied entity for Author:

object(Entities\Book)#179 (3) {
  ["id":"Entities\Book":private]=>
  int(1)
  ["title":"Entities\Book":private]=>
  string(7) "MyBook1"
  ["author":"Entities\Book":private]=>
  object(Doctrine\Proxy\__CG__\Entities\Author)#171 (5) {
  [...]  // many more lines of output

My understanding is that the proxied entity for Author is to be expected, because that is how Doctrine will lazy load information from the author table when I do $book->getAuthor().

Q1: Do you confirm that the presence of the proxied Author entity is expected at this stage?

However what strikes me, is that when I look at the var_dump output (which I've uploaded to pastebin for you to see), it contains more than 10,000 lines! Things I was not expecting to find include references to dummy_table1 and dummy_table2 which are not related to book or author in the model:

["dummy_table1"]=>    // line 1301
object(Doctrine\DBAL\Schema\Table)#194 (10) {

["dummy_table2"]=>   // line 1384
object(Doctrine\DBAL\Schema\Table)#191 (10) {

Q2: Is that expected as well?

From there I was wondering: if I want to store the information contained in $book in cache with serialize to be re-used later on in my views (I'm not talking about doing some operations with $book, just outputting some of the properties), it would be insane as I would store about 500KB for a book title, which brings me to this last question:

Q3: How do you cache the result of your Doctrine queries? Do you serialize the whole entities into cache, do you extract the information you need into an array and then store that array in cache, but if so, doesn't it quickly become cumbersome...?

Community
  • 1
  • 1
Max
  • 12,794
  • 30
  • 90
  • 142

1 Answers1

1

A1: Relations in entities are present at any time(You have written that You get the idea of lazy loading). The relation would be hydrated only when it's demanded.

A2: The huge var_dump data is normal for doctrine entities. Use Doctrine\Common\Util\Debug::dump($entity) instead.

A3: Doctrine has his own caching mechanism for queries and results. I don't think it would be inefficient if You query for the $book again. Furthermore DQL supports array hydration(returns an array rather than an entity).

undefined
  • 2,051
  • 3
  • 26
  • 47
  • about `A3`: since I'm not using `DQL`, array hydration wouldn't help much: does `Doctrine` have a similar tool for converting entities into arrays like `$em->getRepository('Entities\Book')->find(1)->toArray()? (I couldn't find one, so I wrote my own function which works fine and excludes data from relations, but I'm thinking if `Doctrine` had a native way of doing it, it would be more efficient). – Max Oct 25 '12 at 14:07
  • @user359650: Sadly - no. But yes You can write a function that gets You the needed values in the entity class(then You could be able to do the `...->find(1)->BookToArray()` or whatever the function is called). – undefined Oct 25 '12 at 14:12