当前位置:网站首页>TP5 distinct method paging problem

TP5 distinct method paging problem

2022-06-26 04:20:00 Telkobe

DISTINCT Method to return a unique different value , and tp In the use of DISTINCT There are some problems with paging when using the , The total number of paging statistics is inconsistent with the total number of queries, resulting in paging exceptions , The following is for tp5 A few small changes have been made to deal with this problem

First of all, I'll go to Query.php Of paginate Method , And then follow the path

image.png

image.png

image.png

echo Output $aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $field . ') AS tp_' . strtolower($aggregate)

image.png

There is basically no need to look down , Then there is assembly sql Carry out these . Finding this step is easy , Try to put COUNT(*) AS tp_count Just change it to what we want , such as COUNT(DISTINCT xxx) AS tp_count,DISTINCT Must be followed by the field name , You can't just write one *, Will report a mistake , Such as COUNT(DISTINCT user_name) AS tp_count; In order to minimize the impact on the framework itself , So a new function is added to handle , Here is the code .

image.png

image.png

image.png

/**
 * @param $str  Field name , Such as user_name a.user_name
 * @return $this
 */
public function distinct_count($str){
    $this->options['distinct_count'] = $str;
    return $this;
}


/**
 *  Aggregate query 
 * @access public
 * @param  string $aggregate     Polymerization methods 
 * @param  string $field         Field name 
 * @param  bool   $force         Force to numeric type 
 * @return mixed
 */
public function aggregate($aggregate, $field, $force = false)
{
    if (0 === stripos($field, 'DISTINCT ')) {
        list($distinct, $field) = explode(' ', $field);
    }
    if (!preg_match('/^[\w\.\+\-\*]+$/', $field)) {
        throw new Exception('not support data:' . $field);
    }
    if(strlen($this->options['distinct_count'] ?? '')){
        $result = $this->value($aggregate . '(DISTINCT '.$this->options['distinct_count'].') AS tp_' . strtolower($aggregate), 0, $force);
    }else{
        $result = $this->value($aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $field . ') AS tp_' . strtolower($aggregate), 0, $force);
    }
    return $result;
}

原网站

版权声明
本文为[Telkobe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180535384686.html