Hi There,
I would like to get specific columns of two table using ORM. How can i achieve it? I am using belongsTo relationship.
|
Jun '16 |
6 |
517 |
1 |
Hi There,
I would like to get specific columns of two table using ORM. How can i achieve it? I am using belongsTo relationship.
$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();
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.
Yes it will fetch all columns, if you want only specific columns then:
$result = $modelsManager->createBuilder()
->column('columns as string or array')
->from(['modelalias'=>'modelnamespace'])
->leftJoin('joined model',null or condition,'joinalias')
->getQuery()
->execute();
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