sjaakp / yii2-alphapager

Alphabetic paging with Yii 2.0
https://sjaakpriester.nl/software/alphapager
MIT License
18 stars 6 forks source link

Make it work with mongodb #4

Open skworden opened 8 years ago

skworden commented 8 years ago

Cool extension! I thought I'd share how to make this work with mongodb. In the activeDataProvider you need to change two lines in the modifyQuery function. In mongodb the query is the same so you just have to modify the $pattern for the patterns in an array.

protected function modifyQuery(&$query) {
        $attribute = $this->alphaAttribute;
        $pattern = $this->getPattern();
        if ($pattern !== false) {
            if (is_array($pattern)) {
                $pattern = current($pattern);
            }
            $query->orFilterWhere(['like', $attribute, new \MongoRegex('/^' . $pattern . '/')]);
        }
    }