QueryBuilder should return you an iterable object you can iterate like you would a Resultset
. Your Volt template might not need to change at all. You'll need to write a new method and update your controllers to use it instead of findAllTopicsBySql()
.
QueryBuilder is set up to mimic the structure of queries as closely as possible. I suggest you look through the documentation - it's pretty straightforward. An example might be something like:
Thanks a lot !
I've formed the QueryBuilder like below, and it maybe can work:
$builder = $this
->modelsManager
->createBuilder()
->columns(['t.*','u.name','u.avatar','problems_title'=>'p.title'])
->from(['t' => Topics::class])
->innerJoin(Users::class,'u.id = t.users_id', 'u')
->leftJoin(Problems::class,'p.id = t.problems_id', 'p')
->where('t.id IS NOT NULL');
$paginator = new QueryBuilder(
[
"builder" => $builder,
"limit" => 12,
'page' => $numberPage
]
);
$this->view->page = $paginator->paginate();
In the VOLT page, I output the result like this {% for topic in page.getItems() %}
, and I got a strange problem: I can visit $topic->avatar
, $topic->name
and $topic->problems_title
, but I CAN'T visit $topic->users_id
or other fields of topics.
The way $topic->avatar
can output correctly, but $topic->users_id
says FastCGI sent in stderr: "PHP message: PHP Notice: Undefined property: Phalcon\Mvc\Model\Row::$users_id
.
I think maybe it is the problem of QueryBuilder. Below is some parts of results of the var_dump($topic)
:
object(PDOStatement)#106 (1) {
["queryString"]=>
string(702) "SELECT `t`.`id` AS `_t_id`, `t`.`flag` AS `_t_flag`, `t`.`problems_id` AS `_t_problems_id`, `t`.`users_id` AS `_t_users_id`, `t`.`title` AS `_t_title`, `t`.`content` AS `_t_content`, `t`.`reply_count` AS `_t_reply_count`, `t`.`view_count` AS `_t_view_count`, `t`.`favorite_count` AS `_t_favorite_count`, `t`.`vote_count` AS `_t_vote_count`, `t`.`orders` AS `_t_orders`, `t`.`datetimes` AS `_t_datetimes`, `t`.`lastreply_id` AS `_t_lastreply_id`, `u`.`name` AS `name`, `u`.`avatar` AS `avatar`, `p`.`title` AS `problems_title` FROM `topics` AS `t` INNER JOIN `users` AS `u` ON `u`.`id` = `t`.`users_id` LEFT JOIN `problems` AS `p` ON `p`.`id` = `t`.`problems_id` WHERE `t`.`id` IS NOT NULL LIMIT :APL0"
}
["result":protected]=>
NULL
["rowCount":protected]=>
int(12)
["sqlStatement":protected]=>
string(702) "SELECT `t`.`id` AS `_t_id`, `t`.`flag` AS `_t_flag`, `t`.`problems_id` AS `_t_problems_id`, `t`.`users_id` AS `_t_users_id`, `t`.`title` AS `_t_title`, `t`.`content` AS `_t_content`, `t`.`reply_count` AS `_t_reply_count`, `t`.`view_count` AS `_t_view_count`, `t`.`favorite_count` AS `_t_favorite_count`, `t`.`vote_count` AS `_t_vote_count`, `t`.`orders` AS `_t_orders`, `t`.`datetimes` AS `_t_datetimes`, `t`.`lastreply_id` AS `_t_lastreply_id`, `u`.`name` AS `name`, `u`.`avatar` AS `avatar`, `p`.`title` AS `problems_title` FROM `topics` AS `t` INNER JOIN `users` AS `u` ON `u`.`id` = `t`.`users_id` LEFT JOIN `problems` AS `p` ON `p`.`id` = `t`.`problems_id` WHERE `t`.`id` IS NOT NULL LIMIT :APL0"
}
["columnTypes":protected]=>
array(4) {
["t"]=>
array(7) {
["type"]=>
string(6) "object"
["model"]=>
string(18) "YZOI\Models\Topics"
["column"]=>
string(1) "t"
["balias"]=>
string(1) "t"
["instance"]=>
object(YZOI\Models\Topics)#90 (27) {
["id"]=>
NULL
["flag"]=>
NULL
["problems_id"]=>
NULL
["users_id"]=>
NULL
["title"]=>
NULL
["content"]=>
NULL
["reply_count"]=>
NULL
["view_count"]=>
NULL
["favorite_count"]=>
NULL
["vote_count"]=>
NULL
["orders"]=>
NULL
["lastreply_id"]=>
NULL
["container":protected]=>
object(Phalcon\Di\FactoryDefault)#8 (3) {
["services":protected]=>
array(26) {