Model Items:
Код: Выделить всё
class Items extends ActiveRecord {
public static function CollectionName() {
return ['local', 'items'];
}
public function attributes() {
return ['_id', 'name', 'cat_id'];
}
public function rules() {
return [[['_id', 'name', 'cat_id'], 'string']];
}
public function attributeLabels() {
return [...];
}
public function getCat() {
return $this->hasOne(Cats::className(), [(string) '_id' => (string) 'cat_id']);
}
}
Model ItemsSearch:
Код: Выделить всё
class ItemsSearch extends Items {
public function attributes() {
return ['_id', 'name', 'cat_id', 'cat.name'];
}
public function scenarios() {
return Model::scenarios();
}
public function search($params) {
$query = Items::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => ['pageSize' => 30],
]);
$dataProvider->sort->attributes['cat.name'] = [
'asc' => ['cat.name' => SORT_ASC],
'desc' => ['cat.name' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'_id' => $this->_id,
...
]);
$query->andFilterWhere(['like', 'cat.name', $this->getAttribute('cat.name')])
...;
return $dataProvider;
}
}
Код: Выделить всё
class Cats extends ActiveRecord {
public static function collectionName() {
return ['local', 'cats'];
}
public function attributes() {
return [
'_id',
'name',
...
];
}
public function rules() {
return [
[['_id'], 'required'],
[['name'], 'string'],
];
}
}