private function findByPk($db, $type, $columnName = null)
    {
        $needSort = !empty($this->orderBy) && in_array($type, ['All', 'One', 'Column']);
        if ($needSort) {
            if (!is_array($this->orderBy) || count($this->orderBy) > 1) {
                throw new NotSupportedException(
                    'orderBy by multiple columns is not currently supported by redis ActiveRecord.'
                );
            }

            $k = key($this->orderBy);
            $v = $this->orderBy[$k];
            if (is_numeric($k)) {
                $orderColumn = $v;
                $orderType = SORT_ASC;
            } else {
                $orderColumn = $k;
                $orderType = $v;
            }
        }

        if (isset($this->where[0]) && $this->where[0] === 'in') {
            $pks = (array) $this->where[2];
        } elseif (count($this->where) == 1) {
            $pks = (array) reset($this->where);
        } else {
            foreach ($this->where as $values) {
                if (is_array($values)) {
                    // TODO support composite IN for composite PK
                    throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
                }
            }
            $pks = [$this->where];
        }

        /* @var $modelClass ActiveRecord */
        $modelClass = $this->modelClass;

        if ($type === 'Count') {
            $start = 0;
            $limit = null;
        } else {
            $start = ($this->offset === null || $this->offset < 0) ? 0 : $this->offset;
            $limit = ($this->limit < 0) ? null : $this->limit;
        }
        $i = 0;
        $data = [];
        $orderArray = [];
        foreach ($pks as $pk) {
            if (++$i > $start && ($limit === null || $i <= $start + $limit)) {
                $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
                $result = $db->executeCommand('HGETALL', [$key]);
                if (!empty($result)) {
                    $data[] = $result;
                    if ($needSort) {
                        $orderArray[] = $db->executeCommand('HGET', [$key, $orderColumn]);
                    }
                    if ($type === 'One' && $this->orderBy === null) {
                        break;
                    }
                }
            }
        }

        if ($needSort) {
            $resultData = [];
            if ($orderType === SORT_ASC) {
                asort($orderArray, SORT_NATURAL);
            } else {
                arsort($orderArray, SORT_NATURAL);
            }
            foreach ($orderArray as $orderKey => $orderItem) {
                $resultData[] = $data[$orderKey];
            }
            $data = $resultData;
        }

        switch ($type) {
            case 'All':
                return $data;
            case 'One':
                return reset($data);
            case 'Count':
                return count($data);
            case 'Column':
                $column = [];
                foreach ($data as $dataRow) {
                    $row = [];
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        $row[$dataRow[$i++]] = $dataRow[$i++];
                    }
                    $column[] = $row[$columnName];
                }

                return $column;
            case 'Sum':
                $sum = 0;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName) {
                            $sum += $dataRow[$i];
                            break;
                        }
                    }
                }

                return $sum;
            case 'Average':
                $sum = 0;
                $count = 0;
                foreach ($data as $dataRow) {
                    $count++;
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName) {
                            $sum += $dataRow[$i];
                            break;
                        }
                    }
                }

                return $sum / $count;
            case 'Min':
                $min = null;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName && ($min == null || $dataRow[$i] < $min)) {
                            $min = $dataRow[$i];
                            break;
                        }
                    }
                }

                return $min;
            case 'Max':
                $max = null;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName && ($max == null || $dataRow[$i] > $max)) {
                            $max = $dataRow[$i];
                            break;
                        }
                    }
                }

                return $max;
        }
        throw new InvalidParamException('Unknown fetch type: ' . $type);
    }
private function findByPk($db, $type, $columnName = null)
    {
        $needSort = !empty($this->orderBy) && in_array($type, ['All', 'One', 'Column']);
        if ($needSort) {
            if (!is_array($this->orderBy) || count($this->orderBy) > 1) {
                throw new NotSupportedException(
                    'orderBy by multiple columns is not currently supported by redis ActiveRecord.'
                );
            }

            $k = key($this->orderBy);
            $v = $this->orderBy[$k];
            if (is_numeric($k)) {
                $orderColumn = $v;
                $orderType = SORT_ASC;
            } else {
                $orderColumn = $k;
                $orderType = $v;
            }
        }

        if (isset($this->where[0]) && $this->where[0] === 'in') {
            $pks = (array) $this->where[2];
        } elseif (count($this->where) == 1) {
            $pks = (array) reset($this->where);
        } else {
            foreach ($this->where as $values) {
                if (is_array($values)) {
                    
                    throw new NotSupportedException('Find by composite PK is not supported by redis ActiveRecord.');
                }
            }
            $pks = [$this->where];
        }

        
        $modelClass = $this->modelClass;

        if ($type === 'Count') {
            $start = 0;
            $limit = null;
        } else {
            $start = ($this->offset === null || $this->offset < 0) ? 0 : $this->offset;
            $limit = ($this->limit < 0) ? null : $this->limit;
        }
        $i = 0;
        $data = [];
        $orderArray = [];
        foreach ($pks as $pk) {
            if (++$i > $start && ($limit === null || $i <= $start + $limit)) {
                $key = $modelClass::keyPrefix() . ':a:' . $modelClass::buildKey($pk);
                $result = $db->executeCommand('HGETALL', [$key]);
                if (!empty($result)) {
                    $data[] = $result;
                    if ($needSort) {
                        $orderArray[] = $db->executeCommand('HGET', [$key, $orderColumn]);
                    }
                    if ($type === 'One' && $this->orderBy === null) {
                        break;
                    }
                }
            }
        }

        if ($needSort) {
            $resultData = [];
            if ($orderType === SORT_ASC) {
                asort($orderArray, SORT_NATURAL);
            } else {
                arsort($orderArray, SORT_NATURAL);
            }
            foreach ($orderArray as $orderKey => $orderItem) {
                $resultData[] = $data[$orderKey];
            }
            $data = $resultData;
        }

        switch ($type) {
            case 'All':
                return $data;
            case 'One':
                return reset($data);
            case 'Count':
                return count($data);
            case 'Column':
                $column = [];
                foreach ($data as $dataRow) {
                    $row = [];
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        $row[$dataRow[$i++]] = $dataRow[$i++];
                    }
                    $column[] = $row[$columnName];
                }

                return $column;
            case 'Sum':
                $sum = 0;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName) {
                            $sum += $dataRow[$i];
                            break;
                        }
                    }
                }

                return $sum;
            case 'Average':
                $sum = 0;
                $count = 0;
                foreach ($data as $dataRow) {
                    $count++;
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName) {
                            $sum += $dataRow[$i];
                            break;
                        }
                    }
                }

                return $sum / $count;
            case 'Min':
                $min = null;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName && ($min == null || $dataRow[$i] < $min)) {
                            $min = $dataRow[$i];
                            break;
                        }
                    }
                }

                return $min;
            case 'Max':
                $max = null;
                foreach ($data as $dataRow) {
                    $c = count($dataRow);
                    for ($i = 0; $i < $c;) {
                        if ($dataRow[$i++] == $columnName && ($max == null || $dataRow[$i] > $max)) {
                            $max = $dataRow[$i];
                            break;
                        }
                    }
                }

                return $max;
        }
        throw new InvalidParamException('Unknown fetch type: ' . $type);
    }
private function findByPk ( $ db , $ type , $ columnName = null ) { $ needSort = ! empty ( $ this -> orderBy ) && in_array ( $ type , [ 'All' , 'One' , 'Column' ] ) ; if ( $ needSort ) { if ( ! is_array ( $ this -> orderBy ) || count ( $ this -> orderBy ) > 1 ) { throw new NotSupportedException ( 'orderBy by multiple columns is not currently supported by redis ActiveRecord.' ) ; } $ k = key ( $ this -> orderBy ) ; $ v = $ this -> orderBy [ $ k ] ; if ( is_numeric ( $ k ) ) { $ orderColumn = $ v ; $ orderType = SORT_ASC ; } else { $ orderColumn = $ k ; $ orderType = $ v ; } } if ( isset ( $ this -> where [ 0 ] ) && $ this -> where [ 0 ] === 'in' ) { $ pks = ( array ) $ this -> where [ 2 ] ; } elseif ( count ( $ this -> where ) == 1 ) { $ pks = ( array ) reset ( $ this -> where ) ; } else { foreach ( $ this -> where as $ values ) { if ( is_array ( $ values ) ) { // TODO support composite IN for composite PK throw new NotSupportedException ( 'Find by composite PK is not supported by redis ActiveRecord.' ) ; } } $ pks = [ $ this -> where ] ; } /* @var $modelClass ActiveRecord */ $ modelClass = $ this -> modelClass ; if ( $ type === 'Count' ) { $ start = 0 ; $ limit = null ; } else { $ start = ( $ this -> offset === null || $ this -> offset < 0 ) ? 0 : $ this -> offset ; $ limit = ( $ this -> limit < 0 ) ? null : $ this -> limit ; } $ i = 0 ; $ data = [ ] ; $ orderArray = [ ] ; foreach ( $ pks as $ pk ) { if ( ++ $ i > $ start && ( $ limit === null || $ i <= $ start + $ limit ) ) { $ key = $ modelClass :: keyPrefix ( ) . ':a:' . $ modelClass :: buildKey ( $ pk ) ; $ result = $ db -> executeCommand ( 'HGETALL' , [ $ key ] ) ; if ( ! empty ( $ result ) ) { $ data [ ] = $ result ; if ( $ needSort ) { $ orderArray [ ] = $ db -> executeCommand ( 'HGET' , [ $ key , $ orderColumn ] ) ; } if ( $ type === 'One' && $ this -> orderBy === null ) { break ; } } } } if ( $ needSort ) { $ resultData = [ ] ; if ( $ orderType === SORT_ASC ) { asort ( $ orderArray , SORT_NATURAL ) ; } else { arsort ( $ orderArray , SORT_NATURAL ) ; } foreach ( $ orderArray as $ orderKey => $ orderItem ) { $ resultData [ ] = $ data [ $ orderKey ] ; } $ data = $ resultData ; } switch ( $ type ) { case 'All' : return $ data ; case 'One' : return reset ( $ data ) ; case 'Count' : return count ( $ data ) ; case 'Column' : $ column = [ ] ; foreach ( $ data as $ dataRow ) { $ row = [ ] ; $ c = count ( $ dataRow ) ; for ( $ i = 0 ; $ i < $ c ; ) { $ row [ $ dataRow [ $ i ++ ] ] = $ dataRow [ $ i ++ ] ; } $ column [ ] = $ row [ $ columnName ] ; } return $ column ; case 'Sum' : $ sum = 0 ; foreach ( $ data as $ dataRow ) { $ c = count ( $ dataRow ) ; for ( $ i = 0 ; $ i < $ c ; ) { if ( $ dataRow [ $ i ++ ] == $ columnName ) { $ sum += $ dataRow [ $ i ] ; break ; } } } return $ sum ; case 'Average' : $ sum = 0 ; $ count = 0 ; foreach ( $ data as $ dataRow ) { $ count ++ ; $ c = count ( $ dataRow ) ; for ( $ i = 0 ; $ i < $ c ; ) { if ( $ dataRow [ $ i ++ ] == $ columnName ) { $ sum += $ dataRow [ $ i ] ; break ; } } } return $ sum / $ count ; case 'Min' : $ min = null ; foreach ( $ data as $ dataRow ) { $ c = count ( $ dataRow ) ; for ( $ i = 0 ; $ i < $ c ; ) { if ( $ dataRow [ $ i ++ ] == $ columnName && ( $ min == null || $ dataRow [ $ i ] < $ min ) ) { $ min = $ dataRow [ $ i ] ; break ; } } } return $ min ; case 'Max' : $ max = null ; foreach ( $ data as $ dataRow ) { $ c = count ( $ dataRow ) ; for ( $ i = 0 ; $ i < $ c ; ) { if ( $ dataRow [ $ i ++ ] == $ columnName && ( $ max == null || $ dataRow [ $ i ] > $ max ) ) { $ max = $ dataRow [ $ i ] ; break ; } } } return $ max ; } throw new InvalidParamException ( 'Unknown fetch type: ' . $ type ) ; }
Fetch by pk if possible as this is much faster
@param Connection $db the database connection used to execute the query.
If this parameter is not given, the `db` application component will be used.
@param string $type the type of the script to generate
@param string $columnName
@return array|bool|null|string
@throws \yii\base\InvalidParamException
@throws \yii\base\NotSupportedException
Fetch by pk if possible as this is much faster