Developer Blog

  • Blog
  • /
  • Better aliases for eager loading relationships
By Dracony on 30 July 2013

Recently @serfess showed me that the aliases naming for eager loaded tables wasnt really intitive. E.g. lets say you want to load all the fairies from the database together with their home tree and a meadow that this tree grow on. You would do it like this:

1
$pixie->orm->get('fairy')->with('tree.meadow')->find_all();

The problem arises when you want to write a condition such as for example to return only those fairies that live in the trees on a ‘Sunny’ meadow. Since PHPixie was automatically assigning aliases to the joined tables the meadow table would be aliased ‘a2′ which would require you to write the condition as:

1
$pixie->orm->get('fairy')->with('tree.meadow')->where('a2.name', 'Sunny')->find_all();

Which is non intuitive at all.

This has been fixed now, the aliases are named based on the names of the actual relationhips with the ‘.’ substituted by ‘_’. In our example it would be:

1
$pixie->orm->get('fairy')->with('tree.meadow')->where('tree_meadow.name', 'Sunny')->find_all();

Which is much much better =)

comments powered by Disqus