framework icon indicating copy to clipboard operation
framework copied to clipboard

模型一对多hasWhere查询报错

Open owendswang opened this issue 4 years ago • 0 comments

版本:5.1.40 模型blog和click_summary是“一”对“多”的关系,两者模型中设置如下: blog:

public function clicksSummary() { return $this -> hasMany('ClicksSummary', 'blog_id'); }

click_summary:

public function blog() { return $this -> belongsTo('blog'); }

调用: Blogs::hasWhere('clicksSummary', [ 'type' => 0, 'start_time' => $week_start_time ]) -> limit(5) -> order('clicks', 'desc') -> select() -> visible(['id', 'title']) -> toArray();

报错: [10501] PDOException in Connection.php line 687 SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'blog.Blog.title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

            $this->debug(false, '', $master);

            // 返回结果集
            return $this->getResult($pdo, $procedure);
        } catch (\PDOException $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);
            }

            throw new PDOException($e, $this->config, $this->getLastsql());
        } catch (\Throwable $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);
            }

            throw $e;
        } catch (\Exception $e) {
            if ($this->isBreak($e)) {
                return $this->close()->query($sql, $bind, $master, $pdo);

临时解决办法:这个报错之前也有,我通过将/thinkphp/library/think/model/relation/HasMany.php的第334行注释掉,问题就能解决,不知道这个group by有什么特殊用处,我没用对吗?

owendswang avatar Dec 07 '20 07:12 owendswang