Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Accessing joined table?

Since Phalcon doesnt have an option to join tables from the model instead querying the table you want (on the fly) I needed (for performance reasons) for phalcon to join a table straight away.

My code

    public static function getServers($userid)
    {
        return (new self)->getModelsManager()->createBuilder()->from('Users')
            ->where("Users.id = :userid:")
            ->join("Servers", "Users.id = server.owned_by", "server")
            ->getQuery()->execute(['userid' => $userid]);
    }

Then when I try to access $user[0]->server There is no property $server set, How can i access these joined tables?

Or is there a better way for me join the servers table onto the users table in the first query

edited Sep '17

That's correct but im not sure if you don't need to do ->columns("*")



11.6k
Accepted
answer
edited Sep '17

Hi @Jake can you use ->columns(["Users.*", "server.*"]) I'm not sure if is server (alias) or Servers (class name)

Good luck

edited Sep '17

That's correct but im not sure if you don't need to do ->columns("*")

This still throws the

Whoops \ Exception \ ErrorException (E_NOTICE)
Undefined property: Phalcon\Mvc\Model\Row::$server

error

Hi @Jake can you use ->columns(["Users.*", "server.*"]) I'm not sure if is server (alias) or Servers (class name)

Good luck

Thanks your method worked

Is there a way for me to do it without the (new self)->getModelsManager() I did try self::getModelsManager() but thtat just throws Non-static method Phalcon\Mvc\Model::getModelsManager() cannot be called statically

Update yeah there is using self::query() is much cleaner.