Phalcon performance related queries

送分小仙女□ 提交于 2019-11-28 00:26:23

To access ->participants the same way using QueryBuilder, you will have to build join into Query.

Code example could be something like:

$queryBuilder = $this->getDI()->getModelsManager()
    ->createBuilder()
    ->columns(['p.id','participants.*'])
    ->addFrom('Entity\Projects', 'p')
    ->leftJoin('Entity\Participants', 'participants.projectId = p.id', 'participants')
    ->groupBy('p.id, participants.id')
    ->orderBy('p.id ASC');

$resultSet = $queryBuilder->getQuery()->execute();

groupBy() by is used here for making result possibly multi-dimensional.

That kind of query (tested under PgSQL) made Phalcon create some subsequent ResultSet objects of participants pi inside Resultsets for projects p.

You still can iterate through it by using foreach() but after all, I'am not sure it did reduce final query count.

Fireing $result = $resultSet->toArray() made $result['pi'] remain as Resultset, so u should stay cautious on that. You may force it to dump as arrays by defining exact columns in columns() parameters. It has its downside - you will no longer profit from groupBy(), at least on Phalcon 1.3.2 and PHP 5.5.3 im running here.

There is a great library for eager loading on phalcon.

stibiumz phalcon eager loading

That library solves the N + 1 queries for relationships. It is already included on the phalcon incubator. I'm using it on production already.

What it does is create a query using the IN clause and filling the models with the results.

On a has many it does:

SELECT * FROM main
SELECT * FROM related WHERE x.id IN (results from the previous resultset)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!