This question is similar to this one but (literally) takes it to another level, or levels.
Background: I am using the Kohana PHP framework and, specifically, the ORM library.
Basically, when you loop through DB results with it, you can access the field values as properties of an object. Any joined tables can be accessed in a hierarchical manner. For example:
$users = ORM::factory('user')->with('city')->with('city:country')->find_all();
foreach ($users as $user) {
echo "<p>{$user->name} ({$user->city->name}, {$user->city->country->name})</p>";
}
will output:
User 1 (City 1, Country 1)
User 2 (City 2, Country 1)
User 3 (City 2, Country 1)
User 4 (City 3, Country 2)
User 5 (City 4, Country 2)
etc.
Now, my question is: is there a way to access the hierarchical properties of an object for any number of levels. Ideally, I would like to do something like this:
$users = ORM::factory('user')->with('city')->with('city:country')->find_all();
$var2 = 'name';
$var2 = 'city->name';
$var3 = 'city->country->name';
foreach ($users as $user) {
echo "<p>" . $user->{$var1} . "(" . $user->{$var2} . ", " . $user->{$var3} . ")</p>";
}
Is this possible in some simple way that I am missing?
Many thanks!