<?php
namespace common\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\data\Sort;
use yii\helpers\VarDumper;
/**
* ProductSeedsSearch represents the model behind the search form about `common\models\ProductSeeds`.
*/
class ProductSeedsSearch extends ProductSeeds
{
public $sort;
public $category_title;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'status', 'new', 'hit', 'sale', 'stock', 'top_sales', 'top_ten', 'category_id', 'quantity', 'img_id', 'gallery_id', 'brand_id', 'ripeness_group', 'country_id', 'rateNum', 'availability', 'created_at', 'updated_at'], 'integer'],
[['title', 'content', 'vendor_code', 'seo_keywords', 'seo_description', 'category_title', 'sort'], 'safe'],
//[['old_price', 'price'], 'number'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
// $query = ProductSeeds::find();
// add conditions that should always apply here
$id = Yii::$app->request->get('id');
$query = ProductSeeds::find()->joinWith('category')->where(['category_id' => $id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'brand_id' => $this->brand_id,
'country_id' => $this->country_id,
'ripeness_group' => $this->ripeness_group,
]);
$query->andFilterWhere([
'and',
['>=', 'price', Yii::$app->request->get('min_zena')],
['<=', 'price', Yii::$app->request->get('max_zena')]
]);
// if ($this->price) {
// $range = explode(',', $this->price);
//
// $query->andFilterWhere([
// 'and',
// ['>=', 'price', $range[0]],
// ['<=', 'price', $range[1]]
// ]);
// }
return $dataProvider;
}
}
$query = ProductPit::find()->joinWith('category')->where(['category_id'=>$id]);
и фильтрация работает, также в модели продуктов я добавил связь с категории...короче говоря я все по заменял как Вы написали и вроде фильтрует $query->andFilterWhere(['like', 'categoty.title', $this->category_title]);
у меня // grid filtering conditions
$query->andFilterWhere([
'brand_id' => $this->brand_id,
'country_id' => $this->country_id,
'packing' => $this->packing,
]);
if ($this->price) {
$range = explode(',', $this->price);
$query->andFilterWhere([
'and',
['>', 'price', $range[0]],
['<', 'price', $range[1]]
]);
}
<?php
namespace common\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\VarDumper;
/**
* This is the model class for table "product_pit".
*
* @property integer $id
* @property integer $status
* @property integer $new
* @property integer $hit
* @property integer $sale
* @property integer $stock
* @property integer $category_id
* @property integer $img_id
* @property integer $gallery_id
* @property integer $taste_id
* @property integer $brand_id
* @property integer $country_id
* @property string $title
* @property string $content
* @property string $vendor_code
* @property integer $quantity
* @property integer $packing
* @property integer $availability
* @property double $old_price
* @property double $price
* @property string $seo_keywords
* @property string $seo_description
* @property integer $created_at
* @property integer $updated_at
*/
class ProductPit extends ActiveRecord
{
public $taste;
const STATUS_PRODUCT_ON = 1;
const STATUS_PRODUCT_OFF = 0;
const TYPE_IMAGE_MAIN = 1;
const TYPE_IMAGE_GALLERY = 2;
const TOP_TEN_ON = 1;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_pit';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['status', 'new', 'hit', 'sale', 'stock', 'category_id', 'img_id', 'gallery_id', 'taste_id', 'brand_id', 'country_id', 'quantity', 'packing', 'availability', 'created_at', 'updated_at'], 'integer'],
[['content'], 'string'],
[['old_price', 'price'], 'number'],
[['title', 'seo_keywords', 'seo_description'], 'string', 'max' => 255],
[['vendor_code'], 'string', 'max' => 100],
[['taste_array'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'status' => Yii::t('app', 'Status'),
'new' => Yii::t('app', 'New'),
'hit' => Yii::t('app', 'Hit'),
'sale' => Yii::t('app', 'Sale'),
'stock' => Yii::t('app', 'Stock'),
'category_id' => Yii::t('app', 'Category ID'),
'img_id' => Yii::t('app', 'Img ID'),
'gallery_id' => Yii::t('app', 'Gallery ID'),
'taste_id' => Yii::t('app', 'Taste ID'),
//'brand_id' => Yii::t('app', 'Brand ID'),
'country_id' => Yii::t('app', 'Country ID'),
'title' => Yii::t('app', 'Title'),
'content' => Yii::t('app', 'Content'),
'vendor_code' => Yii::t('app', 'Vendor Code'),
'quantity' => Yii::t('app', 'Quantity'),
'packing' => Yii::t('app', 'Packing'),
'availability' => Yii::t('app', 'Availability'),
'old_price' => Yii::t('app', 'Old Price'),
'price' => Yii::t('app', 'Price'),
'seo_keywords' => Yii::t('app', 'Seo Keywords'),
'seo_description' => Yii::t('app', 'Seo Description'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}
/**
* Получаем название категории
* @param $id
* @return mixed
*/
public static function getCategoryTitle($id)
{
return CategoryPit::find()->where(['id' => $id])->one()->title;
}
/**
* Получаем название страны
* @param $id
* @return mixed
*/
public static function getCountryTitleRu($id)
{
return Country::find()->where(['id' => $id])->one()->title_ru;
}
/**
* Получаем название производителя / бренда
* @param $id
* @return mixed
*/
public static function getManufacturesTitle($id)
{
if ($id) {
return ManufacturersPit::find()->where(['id' => $id])->one()->title;
}
}
/**
* Получаем название вкуса
* @param $id
* @return mixed
*/
public static function getTasteTitle($id)
{
return Taste::find()->where(['id' => $id])->one()->title;
}
/**
* Связь с HasProductTaste и получаем все вкусы по id
* @return \yii\db\ActiveQuery
*/
public function getHasProductTaste()
{
return $this->hasMany(HasProductTaste::className(), ['product_id' => 'id']);
}
public function getTastes()
{
return $this->hasMany(Taste::className(), ['id' => 'taste_id'])->via('hasProductTaste');
}
/**
* В view в форме, выводим вкусы для инпута
*/
public function getTagsAsString()
{
$arr = ArrayHelper::map($this->tastes, 'id', 'name');
}
/* Получаем по товары по статусу */
public static function getProductByStatus($id)
{
return self::find()
->where(['category_id' => $id])
->andWhere(['status' => self::STATUS_PRODUCT_ON])
->asArray();
}
/**
* Получаем один товар по id
* @param $id
* @return array|null|ActiveRecord
*/
public static function getOneProductById($id)
{
return self::find()
->where(['id' => $id])
->asArray()
->one();
}
/**
* Получаем для морды товары - гейнер
* @return array|ActiveRecord[]
*/
public static function getGainerProduct()
{
return self::find()
->where(['category_id' => 2])
->andWhere(['status' => self::STATUS_PRODUCT_ON])
->asArray()
->limit('4')
->all();
}
/**
* Получаем для морды товары - BCAA
* @return array|ActiveRecord[]
*/
public static function getBcaaProduct()
{
return self::find()
->where(['category_id' => 3])
->andWhere(['status' => self::STATUS_PRODUCT_ON])
->asArray()
->limit('4')
->all();
}
/**
* Получаем для морды товары - Все протеины
* @return array|ActiveRecord[]
*/
public static function getProteinProduct()
{
return self::find()
->where(['category_id' => 18])
->orWhere(['category_id' => 19])
->orWhere(['category_id' => 20])
->orWhere(['category_id' => 21])
->orWhere(['category_id' => 22])
->orWhere(['category_id' => 23])
->orWhere(['category_id' => 24])
->andWhere(['status' => self::STATUS_PRODUCT_ON])
->andWhere(['top_ten' => self::TOP_TEN_ON])
->asArray()
->limit('10')
->all();
}
/**
* В меню протеины получаем все протеины
*/
public static function getAllProteinProduct($id)
{
return self::find()
->where(['category_id' => $id])
->andWhere(['status' => self::STATUS_PRODUCT_ON]);
}
/**
* Получаем имя главной картинки
* @param $id
* @return mixed
*/
public static function getImageTitle($id)
{
if ($id) {
return ObjectFile::find()->where(['id' => $id])->one()->title;
}
}
/**
* Плучаем имя одной картинки для галереи
* @param $id
* @return array|ActiveRecord[]
*/
public static function getTitleOneImageForGallery($id)
{
if ($id) {
return ObjectFile::find()
->where(['item_id' => $id])
->andWhere(['type' => self::TYPE_IMAGE_GALLERY])
->all();
}
}
}
<?php
namespace common\models;
use Yii;
use yii\db\ActiveRecord;
/**
* This is the model class for table "category_pit".
*
* @property integer $id
* @property integer $parent_id
* @property integer $status
* @property string $title
* @property string $alias
* @property integer $sortOrder
* @property string $seo_keywords
* @property string $seo_description
*/
class CategoryPit extends ActiveRecord
{
public $childs;
const STATUS_CATEGORY_ON = 1;
const STATUS_CATEGORY_OFF = 0;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'category_pit';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['parent_id', 'status', 'sortOrder'], 'integer'],
[['title', 'alias', 'seo_keywords', 'seo_description'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'parent_id' => Yii::t('app', 'Parent ID'),
'status' => Yii::t('app', 'Status'),
'title' => Yii::t('app', 'Title'),
'alias' => Yii::t('app', 'Alias'),
'sortOrder' => Yii::t('app', 'Sort Order'),
'seo_keywords' => Yii::t('app', 'Seo Keywords'),
'seo_description' => Yii::t('app', 'Seo Description'),
];
}
/**
* Строим дерево категории
* @param $data
* @param int $rootID
* @return array
*/
protected function buildTree($data, $rootID = 0)
{
$tree = [];
foreach ($data as $id => $node) {
if ($node['parent_id'] == $rootID) {
unset($data[$id]);
$node['childs'] = $this->buildTree($data, $node['id']);
$tree[] = $node;
}
}
return $tree;
}
/**
* Получаю все категории
* @return array
*/
public function getCategoryForMenu()
{
$data = self::find()->asArray()->all();
return $this->buildTree($data);
}
/**
* Получаю названия категории
* @param $id
* @return mixed
*/
public static function getTitleCategory($id)
{
return self::find()->where(['id' => $id])->one()->title;
}
/**
* @return array|null|ActiveRecord
*/
public static function getCategoryGainer()
{
return self::find()->where(['id' => 2])->one();
}
/**
* Получаем категории из Bcaa
* @return array|null|ActiveRecord
*/
public static function getCategoryBcaa()
{
return self::find()->where(['id' => 3])->one();
}
/**
* @return array|null|ActiveRecord
*/
public static function getCategoryProtein()
{
return self::find()->where(['id' => 1])->one();
}
}
$parentCat = CategoryPit::find()
->where(['parent_id' => $id])
->andWhere(['status' => CategoryPit::STATUS_CATEGORY_ON])
->asArray()
->all();
$result = [];
foreach ($parentCat as $cat) {
$result[] = $cat['id'];
}
if ($id == isset($cat['parent_id'])) {
$query = ProductPit::getAllProteinProduct($result);
} else {
$query = ProductPit::getProductByStatus($id);
}