modules/api/v2/models/search/AdsSearch.php
/**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
Creates data provider instance with search query applied
@param array $params


Creates data provider instance with search query applied @param array $params
public function search($requestParams)
    {
        if (!empty($requestParams['filter'])) {
            foreach ($requestParams['filter'] as $key => $val) {
                if ($key === 'dropdown') {
                    if (is_array($val) && !empty($val)) {
                        foreach ($val as $k => $v) {
                            if (!empty($v['like'])) {
                                $requestParams['filter'][$key][$k]['like'] = trim(urldecode($v['like']));
                            } else {
                                if (isset($v) && !is_array($v) && $v != '') {
                                    $requestParams['filter'][$key][$k] = trim(urldecode($v));
                                }
                            }
                        }
                    }
                } else {
                    if (!empty($val['like'])) {
                        $requestParams['filter'][$key]['like'] = trim(urldecode($val['like']));
                    } else {
                        if (isset($val) && !is_array($val) && $val != '') {
                            $requestParams['filter'][$key] = trim(urldecode($val));
                        }
                    }
                }
            }
        }
        $activeDataFilter = new ActiveDataFilter();
        $activeDataFilter->setSearchModel($this);
        $filter = null;
        if (isset($requestParams['filter']['dropdown'])) {
            unset($requestParams['filter']['dropdown']);
        }
        if ($activeDataFilter !== null) {
            if ($activeDataFilter->load($requestParams)) {
                $filter = $activeDataFilter->build();
                if ($filter === false) {
                    return $activeDataFilter;
                }
            }
        }
        $query = self::find()->where(['status' => Ads::STATUS_ACTIVE]);
        $fields = $this->hiddenFields;
        if (!empty($requestParams['fields'])) {
            $fieldsData = $requestParams['fields'];
            $select = array_diff(explode(',', $fieldsData), $fields);
        } else {
            $select = ['ads.*'];
        }
        $query->select($select);
        if (!empty($filter)) {
            $query->andWhere($filter);
        }
        $query->orderBy(['ads.created_at' => SORT_DESC]);
        $query->groupBy('ads.id');
        $activeDataProvider = Yii::createObject([
            'class' => ActiveDataProvider::class,
            'query' => $query,
            'pagination' => [
                'params' => $requestParams,
                'pageSize' => isset($requestParams['pageSize']) ? $requestParams['pageSize'] : Yii::$app->params['default_page_size'], 
            ],
            'sort' => [
                'params' => $requestParams,
            ],
        ]);
        $adsModelData = $activeDataProvider->getModels();
        foreach ($adsModelData as $key => $value) {
            $adsImage = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
            if (!empty($adsModelData[$key]['image']) && file_exists(Yii::getAlias('@adsImageRelativePath') . '/' . $value->image)) {
                $adsImage = Yii::$app->request->getHostInfo() . Yii::getAlias('@adsImageAbsolutePath') . '/' . $value->image;
            }
            $adsModelData[$key]['image'] = $adsImage;
        }
        $activeDataProvider->setModels($adsModelData);
        return $activeDataProvider;
    }
public function search ( $ requestParams ) { if ( ! empty ( $ requestParams [ 'filter' ] ) ) { foreach ( $ requestParams [ 'filter' ] as $ key => $ val ) { if ( $ key === 'dropdown' ) { if ( is_array ( $ val ) && ! empty ( $ val ) ) { foreach ( $ val as $ k => $ v ) { if ( ! empty ( $ v [ 'like' ] ) ) { $ requestParams [ 'filter' ] [ $ key ] [ $ k ] [ 'like' ] = trim ( urldecode ( $ v [ 'like' ] ) ) ; } else { if ( isset ( $ v ) && ! is_array ( $ v ) && $ v != '' ) { $ requestParams [ 'filter' ] [ $ key ] [ $ k ] = trim ( urldecode ( $ v ) ) ; } } } } } else { if ( ! empty ( $ val [ 'like' ] ) ) { $ requestParams [ 'filter' ] [ $ key ] [ 'like' ] = trim ( urldecode ( $ val [ 'like' ] ) ) ; } else { if ( isset ( $ val ) && ! is_array ( $ val ) && $ val != '' ) { $ requestParams [ 'filter' ] [ $ key ] = trim ( urldecode ( $ val ) ) ; } } } } } $ activeDataFilter = new ActiveDataFilter ( ) ; $ activeDataFilter -> setSearchModel ( $ this ) ; $ filter = null ; if ( isset ( $ requestParams [ 'filter' ] [ 'dropdown' ] ) ) { unset ( $ requestParams [ 'filter' ] [ 'dropdown' ] ) ; } if ( $ activeDataFilter !== null ) { if ( $ activeDataFilter -> load ( $ requestParams ) ) { $ filter = $ activeDataFilter -> build ( ) ; if ( $ filter === false ) { return $ activeDataFilter ; } } } $ query = self :: find ( ) -> where ( [ 'status' => Ads :: STATUS_ACTIVE ] ) ; $ fields = $ this -> hiddenFields ; if ( ! empty ( $ requestParams [ 'fields' ] ) ) { $ fieldsData = $ requestParams [ 'fields' ] ; $ select = array_diff ( explode ( ',' , $ fieldsData ) , $ fields ) ; } else { $ select = [ 'ads.*' ] ; } $ query -> select ( $ select ) ; if ( ! empty ( $ filter ) ) { $ query -> andWhere ( $ filter ) ; } $ query -> orderBy ( [ 'ads.created_at' => SORT_DESC ] ) ; $ query -> groupBy ( 'ads.id' ) ; $ activeDataProvider = Yii :: createObject ( [ 'class' => ActiveDataProvider :: class , 'query' => $ query , 'pagination' => [ 'params' => $ requestParams , 'pageSize' => isset ( $ requestParams [ 'pageSize' ] ) ? $ requestParams [ 'pageSize' ] : Yii :: $ app -> params [ 'default_page_size' ] , ] , 'sort' => [ 'params' => $ requestParams , ] , ] ) ; $ adsModelData = $ activeDataProvider -> getModels ( ) ; foreach ( $ adsModelData as $ key => $ value ) { $ adsImage = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; if ( ! empty ( $ adsModelData [ $ key ] [ 'image' ] ) && file_exists ( Yii :: getAlias ( '@adsImageRelativePath' ) . '/' . $ value -> image ) ) { $ adsImage = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@adsImageAbsolutePath' ) . '/' . $ value -> image ; } $ adsModelData [ $ key ] [ 'image' ] = $ adsImage ; } $ activeDataProvider -> setModels ( $ adsModelData ) ; return $ activeDataProvider ; }
Creates data provider instance with search query applied
 @param array $params
Creates data provider instance with search query applied @param array $params
/* ########## Prepare Request Filter Start ######### */ /* ########## Prepare Request Filter End ######### */ /* ########## Active Data Filter Start ######### */ /* ########## Active Data Filter End ######### */ /* ########## Prepare Query With Default Filter Start ######### */ /* ########## Prepare Query With Default Filter End ######### */ //set page size here