celestialorb
celestialorb

Reputation: 1989

Doctrine2 findAll() method not grabbing associated data?

I have a very simple question. Currently I'm using Doctrine2's findAll() method after the getRepository method and I have a simple OneToOne relationship setup (Manufacturers has a field address_id that links to Addresses, basic stuff). I'm trying to populate a table with one field from the Manufacturer and the rest of the fields with data from the associated Address.

I'm doing this in my view code:

<?php foreach($instances as $instance) { ?>
<?php $address = $instance->address; ?>
  <tr>
    <td><?php echo $instance->name; ?></td>
    <td><?php echo $address->street; ?></td>
    <td><?php echo $address->city; ?></td>
    <td><?php echo $address->state; ?></td>
    <td><?php echo $address->zip; ?></td>
  </tr>
<?php } ?>

where $instances is every entry in the Manufacturers table. Every property is public but for some reason Doctrine2 just won't pull the associated Address data along with the call to findAll() Manufacturers. What am I doing wrong?

This is what I'm using to grab the data:

$instances = $this->doctrine->em->getRepository('Entities\Manufacturer')->findAll();

Do I need to somehow specify to Doctrine that I want to grab associated data as well?

Upvotes: 0

Views: 523

Answers (1)

Bram Gerritsen
Bram Gerritsen

Reputation: 7238

By default all doctrine associations are lazy loading. Doctrine is using proxy objects to make lazy loading of associated data possible. For each field getters are created in the proxy class. You just have to call the getters instead of directly accessing public properties. It is recommended to declare all your fields private in your entity and define getters and setters for them.

Upvotes: 1

Related Questions