开发者

Querying data from a child model

I have 3 models that I have setup thus far in a simple application I am working on:

So far I have these models:

  • UserAccountEntity - Top level Table (Has a One-Many Relationship to UserAccountEntityStrings)
  • UserAccountEntityStrings - Child Table (Has a Many-One relation ship to UserAccountEntity and EavAttributes
  • EavAttributes - Lookup Table

When I query data from my top level table, I get the schema,association information for the child table. But I do not get any of the persisted data from the child table.

What I expected the results to be were, the data from the top level model and the data from the associated child model. Any help with this is greatly appreciated.

A note that may be helpful, I am using Zend 1.11.10 and Doctrine 2

This is what my query looks like:

   $users = $em->createQuery('select u from Fiobox\Entity\UserModule\UserAccountEntity u')->execute();
   Zend_Debug::dump($users[0]);

This is the association in my top level model:

/**
 *
 * @param \Doctrine\Common\Collections\Collection $property
 * @OneToMany(targetEntity="UserAccountEntityStrings",mappedBy="UserAccountEntity", cascade={"persist","remove"})
 */
private $strings;

These are the associations in my child model:

/**
 *
 * @var UserAccountEn开发者_StackOverflow社区tity
 * @ManyToOne(targetEntity="UserAccountEntity")
 * @JoinColumns({
 *  @JoinColumn(name="entity_id", referencedColumnName="entity_id")
 * })
 */
private $user;

/**
 * @var EavAttribute
 * @ManyToOne(targetEntity="Fiobox\Entity\EavModule\EavAttributes")
 * @JoinColumn(name="attribute_id", referencedColumnName="attribute_id")
 */
private $attributes;


Have you actually tried anything?

Doctrine will lazy load stuff for you. Your var_dump probably shows persistent collections of proxy objects for your child objects. But if you access them, they'll be loaded automatically:

<?php
$users = $em->createQuery('select u from Fiobox\Entity\UserModule\UserAccountEntity u')->fetchAll();

foreach($users as $u){
    foreach($u->strings as $s){
        var_dump($s);
    }
}

If you know that you're going to need all that child data, you might as well force a fetch-join in your DQL:

<?php
$users = $em->createQuery('select u, s from Fiobox\Entity\UserModule\UserAccountEntity u JOIN u.strings s')->fetchAll();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜