I've got a simple pair of models like this:
class Users extends Model
{
public $id;
public $name;
public $role_id;
}
class Roles extends Model
{
public $id;
public $role_name;
}
The role id in the Users model relates to the id in the Roles model.
I want to do a simple query with a join, like this:
$query = $this->modelsManager->createBuilder()
->columns(["u.*", "r.role_name"])
->from(["r" => "Roles"])
->join("Users", "u.role_id = r.id", "u")
->where("u.name = :name:")
->getQuery()
->getSingleResult(["name" => $username]);
This is generating the correct SQL code (I've turned on SQL logging) and it's also generating the correct result. But I can't figure out how to extract the role name from the result. Various Phalcon documentation and examples I've read imply that it's something like
foreach ($query as $user) {
error_log ("user name is " . $user->name);
error_log ("role name is " . $user->r->role_name);
}
but that fails with an error that r isn't a property of Users (and neither is role name, if I leave out the r->).
I'm sure this is pretty simple, but it's got me beat. Any help appreciated!