-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2.0.10: Fatal error with Query Builder + Count + Group By + Paginator #11395
Comments
UPD: Upgraded to 2.0.10, and this code: use Phalcon\Paginator\Adapter\QueryBuilder as PaginatorModel;
$data = $this->getModelsManager()->createBuilder()
->from(array('a' => '\Db\Article'))
->columns(array(
'a.*',
'COUNT(ac.id) AS c'
))
->leftJoin('\Db\ArticleComment', 'a.id = ac.article_id', 'ac')
->where('a.status = :status:', array('status' => 'approved'))
->orderBy(array('a.date_post DESC', 'a.date_add DESC'))
->groupBy('a.id');
;
$paginator = new PaginatorModel(array(
'builder' => $data,
'limit' => 10,
'page' => $page
));
$paginate = $paginator->getPaginate();
var_dump($paginate->total_items); die; causes an error:
|
@hailie-rei Can you please test now |
@sergeyklay I followed these steps:
Please correct me if I was wrong. For my steps I received an error: Warning: Phalcon\Paginator\Adapter\QueryBuilder::getPaginate(): Invalid arguments supplied for fast_join() in /Users/Lena/redlab/www/japanese/app/modules/Article/Models/Article.php on line 32 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AS Stack trace: |
@hailie-rei Right now I tested this on Phalcon Forum DB schema: https://github.com/phalcon/forum/blob/2.0.x/schemas/forum.sql /** @var \Phalcon\Mvc\Model\Query\Builder $builder */
$builder = $this->modelsManager->createBuilder()
->from(['p' => 'Phosphorum\Models\Posts'])
->columns([
'p.*',
'COUNT(p.id) AS c',
])
->leftJoin('Phosphorum\Models\PostsReplies', "p.id = pr.posts_id", 'pr')
->where('p.status = :status:', ['status' => 'A'])
->orderBy(['p.created_at DESC', 'p.edited_at DESC'])
->groupBy('p.id');
$paginator = new PaginatorModel([
'builder' => $builder,
'limit' => 10,
'page' => 1
]);
$paginate = $paginator->getPaginate();
var_dump($paginate->total_items);
/** @var \Phalcon\Mvc\Model\Query $query */
$query = $builder->getQuery();
print_r($query->getSql()['sql']); die; Result: Total items: 415 SELECT
`p`.`id` AS `_p_id`,
`p`.`users_id` AS `_p_users_id`,
`p`.`categories_id` AS `_p_categories_id`,
`p`.`title` AS `_p_title`,
`p`.`slug` AS `_p_slug`,
`p`.`content` AS `_p_content`,
`p`.`number_views` AS `_p_number_views`,
`p`.`number_replies` AS `_p_number_replies`,
`p`.`votes_up` AS `_p_votes_up`,
`p`.`votes_down` AS `_p_votes_down`,
`p`.`sticked` AS `_p_sticked`,
`p`.`created_at` AS `_p_created_at`,
`p`.`modified_at` AS `_p_modified_at`,
`p`.`edited_at` AS `_p_edited_at`,
`p`.`status` AS `_p_status`,
`p`.`locked` AS `_p_locked`,
`p`.`deleted` AS `_p_deleted`,
`p`.`accepted_answer` AS `_p_accepted_answer`,
COUNT(`p`.`id`) AS `c`
FROM
`posts` AS `p`
LEFT JOIN
`posts_replies` AS `pr`
ON
`p`.`id` = `pr`.`posts_id`
WHERE
`p`.`status` = :status
GROUP BY
`p`.`id`
ORDER BY
`p`.`created_at` DESC,
`p`.`edited_at` DESC
Latest commit: 932eae0 |
I have same problem with my forum treeview schema CREATE TABLE IF NOT EXISTS `forum` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`parent` int(11) unsigned DEFAULT NULL,
`user_id` int(11) unsigned DEFAULT NULL,
`user_name` varchar(50) DEFAULT NULL,
`title` varchar(500) DEFAULT NULL,
`message` text,
`create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `FK_forunt_forunt` (`parent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query builder $builder = $this->modelsManager->createBuilder()
->from(['t' => 'Forum'])
->columns(['t.*', 'COUNT(p.id) AS count'])
->leftJoin('Forum', 't.id = p.parent', 'p')
->where('t.parent = :id:', ['id' => $id])
->orderBy('t.update_at desc, t.create_at desc')
->groupBy('t.id'); Result total_items = 0 SELECT `t`.`id` AS `_t_id`, `t`.`parent` AS `_t_parent`, `t`.`user_id` AS `_t_user_id`, `t`.`user_name` AS `_t_user_name`, `t`.`title` AS `_t_title`, `t`.`message` AS `_t_message`, `t`.`create_at` AS `_t_create_at`, `t`.`update_at` AS `_t_update_at`, COUNT(`p`.`id`) AS `count` FROM `forum` AS `t` LEFT JOIN `forum` AS `p` ON `t`.`id` = `p`.`parent` WHERE `t`.`parent` = :id GROUP BY `t`.`id` ORDER BY `t`.`update_at` DESC, `t`.`create_at` DESC version - 2.1.0 RC 1 |
@hailie-rei @Gigabiter Could you please test 2.1.x branch? |
Where I can download phalcon 2.1.x windows dll for my local Openserver PHP 5.6? |
@sergeyklay $result->total_items = 0
$query->getSql()['sql'] = 'SELECT `t`.`id` AS `_t_id`, `t`.`parent` AS `_t_parent`, `t`.`user_id` AS
`_t_user_id`, `t`.`user_name` AS `_t_user_name`, `t`.`title` AS `_t_title`, `t`.`message` AS
`_t_message`, `t`.`create_at` AS `_t_create_at`, `t`.`update_at` AS `_t_update_at`, COUNT(`p`.`id`)
AS `count` FROM `forum` AS `t` LEFT JOIN `forum` AS `p` ON `t`.`id` = `p`.`parent` WHERE
`t`.`parent` = :id GROUP BY `t`.`id` ORDER BY `t`.`update_at` DESC, `t`.`create_at` DESC' 2.1.0 RC1 did not include your fixes? |
Windows DLLs are here https://phalconphp.com/en/download/windows |
@sergeyklay Code well-earned! Дякую! ) |
@sergeyklay
== See the next comment ==
I use Phalcon 2.0.8.
returns: int(1)
Working example with data instead of query builder object for Paginator:
returns: int(62)
My tables:
I guess problem is somewhere here: https://github.com/phalcon/cphalcon/blob/master/phalcon/paginator/adapter/querybuilder.zep#L189
Just to compare: https://github.com/phalcon/cphalcon/blob/master/phalcon/paginator/adapter/model.zep#L97 n is total items count. This code works.
The text was updated successfully, but these errors were encountered: