简体   繁体   English

MySQL查询未在Phalcon中执行

[英]MySQL query not executing in phalcon

Using phalcon framework 使用Phalcon框架

The purpose of this mysql query is to return the comment count on a forum within the last 1 day: 此mysql查询的目的是在最近1天内在论坛上返回评论数:

SELECT fc.forum_id, count( fc.forum_id ) AS total_comments
FROM forum_comments AS fc
WHERE DATE_SUB( CURDATE( ) , INTERVAL 1
DAY ) <= fc.updated_at
GROUP BY fc.forum_id
ORDER BY total_comments DESC
LIMIT 0 , 30

It will run correctly in phpmyadmin but phalcon throws: 它将在phpmyadmin中正确运行,但是phalcon抛出:

Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\sidra\\app\\controllers\\ApiController.php on line 810 Syntax error, unexpected token INTEGER(1), near to ' DAY ) <= fc.updated_at GROUP BY fc.forum_id ORDER BY total_comments DESC ', when parsing: SELECT fc.forum_id,count(fc.forum_id)as total_comments FROM ForumComments as fc WHERE DATE_SUB( CURDATE( ) , INTERVAL 1 DAY ) <= fc.updated_at GROUP BY fc.forum_id ORDER BY total_comments DESC (215) 注意:尝试在第810行上的C:\\ xampp \\ htdocs \\ sidra \\ app \\ controllers \\ ApiController.php中获取非对象的属性语法错误,意外令牌INTEGER(1),靠近'DAY)<= fc.updated_at GROUP BY fc.forum_id ORDER BY total_comments DESC',在解析时:从论坛评论中选择fc.forum_id,count(fc.forum_id)as total_comments作为fc WHERE DATE_SUB(CURDATE(),间隔1天)<= fc.updated_at GROUP BY fc .forum_id ORDER BY total_comments DESC(215)

My phalcon codes: 我的Phalcon代码:

$sql = "SELECT fc.forum_id,count(fc.forum_id)as total_comments ".
                "FROM ForumComments as fc ".
                "WHERE DATE_SUB( CURDATE( ) , INTERVAL 1
                 DAY ) <= fc.updated_at ".
                "GROUP BY fc.forum_id ".
                "ORDER BY total_comments DESC ";

        $query = $this->modelsManager->createQuery($sql);
        $staff = $query->execute()->toArray();

If there any alternative way to do this? 是否有其他替代方法?

Your query is correct and works fine on MySQL versions from 4.1 to 5.6.6. 您的查询是正确的,并且可以在4.1到5.6.6的MySQL版本上正常工作。 So it seems that createQuery() method is trying to parse the query and fails on the INTERVAL clause. 因此,似乎createQuery()方法正在尝试解析查询,并且在INTERVAL子句上失败。 I would recommend you to try something like this: 我建议您尝试这样的事情:

$yesterday = date('Y-m-d', strtotime("-1 days"));

$sql = "SELECT fc.forum_id,count(fc.forum_id)as total_comments ".
            "FROM ForumComments as fc ".
            "WHERE '$yesterday' <= fc.updated_at ".
            "GROUP BY fc.forum_id ".
            "ORDER BY total_comments DESC ";

$query = $this->modelsManager->createQuery($sql);
$staff = $query->execute()->toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM