tags:

views:

39

answers:

1

So I have two tables: X and Y.

The schema.yml looks like:

X: 
  columns:
    something: { type: string(255), notnull: true }
    y_id: { type: integer, notnull: true }
  relations:
    Y: { onDelete: CASCADE, local: y_id, foreign: id, foreignAlias: xs }
Y:
  columns:
    something: { type: string(255), notnull: true }

And in my XTable I have:

class XTable extends Doctrine_Table
{
    function getXesWithYs()
    {
        return $this->createQuery("x")->leftJoin("x.Y y")->execute();
    }
}

The generated query does in fact select both x.* and y.*, but when I try using Y's data, it performs an extra query for every row selecting from Y where y.id = x.y_id.

So if I do:

echo $results[0]->Y->something;

Doctrine will run an extra query fetching Y's data again.

This only happens once per Y row. If I use it again it won't load it a 3rd time, but if I use $result[1]'s Y, I get another query.

+1  A: 

Ah doctrine lazy loading at its most stupid or at least not very well documented.

It seems that even if you join something in like you have done you must also select those tables. Like so:

class XTable extends Doctrine_Table
{
    function getXesWithYs()
    {
        return $this->createQuery("x")->select('x.*, y.*')->leftJoin("x.Y y")->execute();
    }
}
johnwards