We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can i get specific columns of two table using ORM?

Hi There,

I would like to get specific columns of two table using ORM. How can i achieve it? I am using belongsTo relationship.

edited Jun '16
$this->belongsTo('site_id', 'Model\Site', 'id', ['alias'=>'Site']);
$this->belongsTo('article_id', 'Model\Article', 'id', ['alias'=>'Article']);
$siteId = $modelInstance->Site->getId();
$articleId = $modelInstance->Article->getId();

You should use query builder and joins.

Thank you for the comment.

Right now i am using this way. My concern is that, I have more than 2 lac records with relationship of other table. so when i use PHQL it will take much time. I think because of PHQL retrives all columns of both table by default. so it may be taking more times.

What should i do instead of fetching all column, will fetch specific column?

In your example, it will fetch all columns right? I do not want that. i just want to fetch specific columns when my query is executed.

edited Jun '16

Yes it will fetch all columns, if you want only specific columns then:

$result = $modelsManager->createBuilder()
->column('columns as string or array')
->leftJoin('joined model',null or condition,'joinalias')

You will get rid of creating full objects as well. Selecting full objects will slow down whole query etc a bit.

Hi There,

Thank you for reply. I am finding that solution. I got. Thanks

Wait, there is a huge difference between ORM (Object Relational Mapping) and PHQL (Phalcon Query Language).

Think of it as this: Raw SQL < PDO < PHQL < ORM

@jurigag is right in that by using a builder and joins will produce a single query, hence will be faster.

I was trying to answer your question, which was about ORM

Well, still ORM using PHQL which uses PDO :D

.. do you even read bro? :P

Your answering is appriaciatable. :)