Reputation: 113
I've picked up Phalcon framework because of the best declared performance. I was trying to analyse my application and discovered something really weird. I am using version 4.1.0.
Please advise if I am doing something wrong in here or is it a bug in Phalcon - as it's what it seems to me.
In the controller I prepare (a vary standard way I guess) a variable for a view taken from the DB request (by PHQL in Model->Categories->getAvailableCategories()). This query is not very well optimised yet, so it returns the results in 60 msec right now.
$c = new Categories(); // Model
$availableCategories = $c->getAvailableCategories(... some optional variables ...);
$this->view->setVar('availableCategories', $availableCategories);
The problem I discovered is that when I run a loop in the phtml view I get another 60 msec to get content for the $availableCategores variable
// inside the PHTML view
<?php foreach ($availableCategories as $category): ?>
It takes another 60 msec to get this line in a view and I can see in the DB logs that this underlying query has been executed twice.
This sound like a bug to me.
I do not see anything in documentation to use a data prepared and collected already in the variable inside the controller. This make preparation of any data in a controller pointless if queries need to run again when requested from a view. It simply doubles the whole execution time.
Upvotes: 0
Views: 360
Reputation: 113
My problem is that by following Phalcon documentation inside of the getAvailableCategories
$records = $this
->modelsManager
->executeQuery($phql);
I was expecting that $records variable would hold the results of executed query, right?
Instead the $records is the object of Phalcon\Mvc\Model\Resultset\Simple type. When it's passed by a variable to a View and when you would try to get results in a loop foreach ($records as $row):
the query is definitely executed again. I've confirmed this also in latest version of PHP 7.4.28 and Phalcon 4.1.2.
The solution is add ->toArray()
to the end of so only real records data would be passed and no need for a second call.
$records = $this
->modelsManager
->executeQuery($phql)
->toArray();
Upvotes: 0