1. Go to this page and download the library: Download yii2tech/ar-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2tech / ar-search example snippets
use yii2tech\ar\search\ActiveSearchModel;
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item'
]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
namespace app\models;
// ActiveRecord to be searched:
class Item extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['name', 'status', 'price'], 'd ActiveRecord:
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item'
]);
// safe attributes of `Item` are inherited:
$searchModel->name = 'Paul';
$searchModel->price = 10.5;
use yii2tech\ar\search\ActiveSearchModel;
use yii\widgets\ActiveForm;
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item'
]);
use yii2tech\ar\search\ActiveSearchModel;
use yii2tech\ar\search\ActiveSearchEvent;
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item',
]);
$searchModel->on(ActiveSearchModel::EVENT_AFTER_CREATE_QUERY, function(ActiveSearchEvent $event) {
$event->query
->with(['category'])
->andWhere(['status' => 1]);
});
use yii2tech\ar\search\ActiveSearchModel;
use yii\data\ActiveDataProvider;
use app\models\Item;
$searchModel = new ActiveSearchModel([
'model' => Item::className(),
'dataProvider' => function () {
$query = Item::find()
->with(['category'])
->andWhere(['status' => 1]);
return ActiveDataProvider(['query' => $query]);
},
]);
use yii2tech\ar\search\ActiveSearchModel;
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item',
'filterOperators' => [
ActiveSearchModel::TYPE_STRING => '=', // use strict comparison for the string attributes
ActiveSearchModel::TYPE_INTEGER => function (\yii\db\ActiveQueryInterface $query, $attribute, $value) {
if ($attribute === 'commentsCount') {
$query->andHaving(['commentsCount' => $value]);
} else {
$query->andFilterWhere([$attribute => $value]);
}
},
],
]);
use yii2tech\ar\search\ActiveSearchModel;
$searchModel = new ActiveSearchModel([
'model' => 'app\models\Item',
'compareAllowedAttributes' => [
'price' // allow compare for 'price' only, excluding such fields like 'categoryId', 'status' and so on.
],
]);