开发者

Querying data from a child model

开发者 https://www.devze.com 2023-04-07 05:52 出处:网络
I have 3 models that I have setup thus far in a simple application I am working on: So far I have these models:

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

精彩评论

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

关注公众号