common/services/base/ActiveRecordService.php
public static function findPageForSearch( $config = [] ) {

		$modelClass	= static::$modelClass;
		$modelTable = $modelClass::tableName();

		$parentType	= $config[ 'parentType' ] ?? static::$parentType;

		// Search in
		$searchModel	 	= $config[ 'searchModel' ] ?? true; // Search in model name
		$searchCategory 	= $config[ 'searchCategory' ] ?? false; // Search in category name
		$searchTag		 	= $config[ 'searchTag' ] ?? false; // Search in tag name

		// DB Tables
		$mcategoryTable		= CoreTables::TABLE_MODEL_CATEGORY;
		$categoryTable		= CoreTables::TABLE_CATEGORY;
		$mtagTable			= CoreTables::TABLE_MODEL_TAG;
		$tagTable			= CoreTables::TABLE_TAG;

		// Sort
		$sortParam	= Yii::$app->request->get( 'sort' );
		$sortParam	= preg_replace( '/-/', '', $sortParam );

		// Keywords
		$searchParam	= isset( $config[ 'search-param' ] ) ? $config[ 'search-param' ] : 'keywords';
		$keywords		= Yii::$app->request->getQueryParam( $searchParam );

		// Search Query
		$query	= $config[ 'query' ] ?? $modelClass::find();
		$hasOne	= $config[ 'hasOne' ] ?? false;

		// Use model joins
		if( $hasOne ) {

			$query = $modelClass::queryWithHasOne();
		}

		// Tag
		if( $searchTag || isset( $config[ 'tag' ] ) || $sortParam === 'tag' ) {

			$query->leftJoin( $mtagTable, "$modelTable.id=$mtagTable.parentId AND $mtagTable.parentType='$parentType' AND $mtagTable.active=TRUE" )
				->leftJoin( $tagTable, "$mtagTable.modelId=$tagTable.id" );
		}

		if( isset( $config[ 'tag' ] ) ) {

			$query->andWhere( "$tagTable.id=" . $config[ 'tag' ]->id );
		}

		// Category
		if( $searchCategory || isset( $config[ 'category' ] ) || $sortParam === 'category' ) {

			$query->leftJoin( $mcategoryTable, "$modelTable.id=$mcategoryTable.parentId AND $mcategoryTable.parentType='$parentType' AND $mcategoryTable.active=TRUE" )
				->leftJoin( $categoryTable, "$mcategoryTable.modelId=$categoryTable.id" );
		}

		if( isset( $config[ 'category' ] ) ) {

			$query->andWhere( "$categoryTable.id=" . $config[ 'category' ]->id );
		}

		// Search
		if( isset( $keywords ) ) {

			if( $searchModel ) {

				$config[ 'search-col' ][] = "$modelTable.name";
			}

			if( $searchCategory ) {

				$config[ 'search-col' ][] = "$categoryTable.name";
			}

			if( $searchTag ) {

				$config[ 'search-col' ][] = "$tagTable.name";
			}
		}

		// Group by model id
		$query->groupBy( "$modelTable.id" );

		$config[ 'query' ] = $query;

		return static::findPage( $config );
	}
public static function findPageForSearch( $config = [] ) {

		$modelClass	= static::$modelClass;
		$modelTable = $modelClass::tableName();

		$parentType	= $config[ 'parentType' ] ?? static::$parentType;

		// Search in
		$searchModel	 	= $config[ 'searchModel' ] ?? true; // Search in model name
		$searchCategory 	= $config[ 'searchCategory' ] ?? false; // Search in category name
		$searchTag		 	= $config[ 'searchTag' ] ?? false; // Search in tag name

		// DB Tables
		$mcategoryTable		= CoreTables::TABLE_MODEL_CATEGORY;
		$categoryTable		= CoreTables::TABLE_CATEGORY;
		$mtagTable			= CoreTables::TABLE_MODEL_TAG;
		$tagTable			= CoreTables::TABLE_TAG;

		// Sort
		$sortParam	= Yii::$app->request->get( 'sort' );
		$sortParam	= preg_replace( '/-/', '', $sortParam );

		// Keywords
		$searchParam	= isset( $config[ 'search-param' ] ) ? $config[ 'search-param' ] : 'keywords';
		$keywords		= Yii::$app->request->getQueryParam( $searchParam );

		// Search Query
		$query	= $config[ 'query' ] ?? $modelClass::find();
		$hasOne	= $config[ 'hasOne' ] ?? false;

		// Use model joins
		if( $hasOne ) {

			$query = $modelClass::queryWithHasOne();
		}

		// Tag
		if( $searchTag || isset( $config[ 'tag' ] ) || $sortParam === 'tag' ) {

			$query->leftJoin( $mtagTable, "$modelTable.id=$mtagTable.parentId AND $mtagTable.parentType='$parentType' AND $mtagTable.active=TRUE" )
				->leftJoin( $tagTable, "$mtagTable.modelId=$tagTable.id" );
		}

		if( isset( $config[ 'tag' ] ) ) {

			$query->andWhere( "$tagTable.id=" . $config[ 'tag' ]->id );
		}

		// Category
		if( $searchCategory || isset( $config[ 'category' ] ) || $sortParam === 'category' ) {

			$query->leftJoin( $mcategoryTable, "$modelTable.id=$mcategoryTable.parentId AND $mcategoryTable.parentType='$parentType' AND $mcategoryTable.active=TRUE" )
				->leftJoin( $categoryTable, "$mcategoryTable.modelId=$categoryTable.id" );
		}

		if( isset( $config[ 'category' ] ) ) {

			$query->andWhere( "$categoryTable.id=" . $config[ 'category' ]->id );
		}

		// Search
		if( isset( $keywords ) ) {

			if( $searchModel ) {

				$config[ 'search-col' ][] = "$modelTable.name";
			}

			if( $searchCategory ) {

				$config[ 'search-col' ][] = "$categoryTable.name";
			}

			if( $searchTag ) {

				$config[ 'search-col' ][] = "$tagTable.name";
			}
		}

		// Group by model id
		$query->groupBy( "$modelTable.id" );

		$config[ 'query' ] = $query;

		return static::findPage( $config );
	}
public static function findPageForSearch ( $ config = [ ] ) { $ modelClass = static :: $ modelClass ; $ modelTable = $ modelClass :: tableName ( ) ; $ parentType = $ config [ 'parentType' ] ?? static :: $ parentType ; // Search in $ searchModel = $ config [ 'searchModel' ] ?? true ; // Search in model name $ searchCategory = $ config [ 'searchCategory' ] ?? false ; // Search in category name $ searchTag = $ config [ 'searchTag' ] ?? false ; // Search in tag name // DB Tables $ mcategoryTable = CoreTables :: TABLE_MODEL_CATEGORY ; $ categoryTable = CoreTables :: TABLE_CATEGORY ; $ mtagTable = CoreTables :: TABLE_MODEL_TAG ; $ tagTable = CoreTables :: TABLE_TAG ; // Sort $ sortParam = Yii :: $ app -> request -> get ( 'sort' ) ; $ sortParam = preg_replace ( '/-/' , '' , $ sortParam ) ; // Keywords $ searchParam = isset ( $ config [ 'search-param' ] ) ? $ config [ 'search-param' ] : 'keywords' ; $ keywords = Yii :: $ app -> request -> getQueryParam ( $ searchParam ) ; // Search Query $ query = $ config [ 'query' ] ?? $ modelClass :: find ( ) ; $ hasOne = $ config [ 'hasOne' ] ?? false ; // Use model joins if ( $ hasOne ) { $ query = $ modelClass :: queryWithHasOne ( ) ; } // Tag if ( $ searchTag || isset ( $ config [ 'tag' ] ) || $ sortParam === 'tag' ) { $ query -> leftJoin ( $ mtagTable , "$modelTable.id=$mtagTable.parentId AND $mtagTable.parentType='$parentType' AND $mtagTable.active=TRUE" ) -> leftJoin ( $ tagTable , "$mtagTable.modelId=$tagTable.id" ) ; } if ( isset ( $ config [ 'tag' ] ) ) { $ query -> andWhere ( "$tagTable.id=" . $ config [ 'tag' ] -> id ) ; } // Category if ( $ searchCategory || isset ( $ config [ 'category' ] ) || $ sortParam === 'category' ) { $ query -> leftJoin ( $ mcategoryTable , "$modelTable.id=$mcategoryTable.parentId AND $mcategoryTable.parentType='$parentType' AND $mcategoryTable.active=TRUE" ) -> leftJoin ( $ categoryTable , "$mcategoryTable.modelId=$categoryTable.id" ) ; } if ( isset ( $ config [ 'category' ] ) ) { $ query -> andWhere ( "$categoryTable.id=" . $ config [ 'category' ] -> id ) ; } // Search if ( isset ( $ keywords ) ) { if ( $ searchModel ) { $ config [ 'search-col' ] [ ] = "$modelTable.name" ; } if ( $ searchCategory ) { $ config [ 'search-col' ] [ ] = "$categoryTable.name" ; } if ( $ searchTag ) { $ config [ 'search-col' ] [ ] = "$tagTable.name" ; } } // Group by model id $ query -> groupBy ( "$modelTable.id" ) ; $ config [ 'query' ] = $ query ; return static :: findPage ( $ config ) ; }
Generate search query using tag and category tables. The search will be done in model, category and tag names.
Generate search query using tag and category tables . The search will be done in model category and tag names .