我的学说查询 returns 有不同的错误

My doctrine query returns error with distrinct

我正在尝试 return 我的访问,但我需要一个独特的而不是计数。我尝试了这些存储库方法,但仅计数有效。

工作:

public function numberOfVisitsBetweenDates($dateMin, $dateMax)
    {
        $builder = $this->createQueryBuilder('visit');
        $builder->select('count(DISTINCT visit.user)');
        $builder->where('visit.date BETWEEN :dateMin AND :dateMax');
        $builder->setParameter('dateMin', $dateMin);
        $builder->setParameter('dateMax', $dateMax);
        $query = $builder->getQuery();
        // dd($query);
    
            return $query->getResult();
    }

My dump dql -_dql: "SELECT count(DISTINCT visit.user) FROM App\Entity\Visit visit WHERE visit.date BETWEEN :dateMin AND :dateMax


不工作:

    public function numberOfVisitsBetweenDates($dateMin, $dateMax)
        {
            $builder = $this->createQueryBuilder('visit');
            $builder->select('visit.user');
            $builder->where('visit.date BETWEEN :dateMin AND :dateMax');
            $builder->setParameter('dateMin', $dateMin);
            $builder->setParameter('dateMax', $dateMax);
            $builder->distinct();
    
            $query = $builder->getQuery();
            // dd($query);
        
                return $query->getResult();
        }

我的转储 dql: -_dql: "SELECT DISTINCT visit.user FROM App\Entity\Visit 访问 WHERE visit.date在 :dateMin 和 :dateMax 之间 我的错误:

QueryException QueryException HTTP 500 Internal Server Error [Semantical Error] line 0, col 22 near 'user FROM App\Entity\Visit': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

两个代码几乎一样,我找不到解决方案

你应该尝试这样的事情:

    public function numberOfVisitsBetweenDates($dateMin, $dateMax)
        {
            $builder = $this->createQueryBuilder('visit');
            $builder->where('visit.date BETWEEN :dateMin AND :dateMax');
            $builder->setParameter('dateMin', $dateMin);
            $builder->setParameter('dateMax', $dateMax);
            $builder->groupBy('visit.user');
    
            $query = $builder->getQuery();
            return $query->getResult();
        }