/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ProductType::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['parentName'] = ['asc' => ['parent.name' => SORT_ASC], 'desc' => ['parent.name' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { $dataProvider->getSort() !== false ? $query->joinWith(['parentRelation']) : $query->with('parentRelation'); return $dataProvider; } else { $query->joinWith(['parentRelation']); } $query->andFilterWhere(['like', '{{%product_types}}.name', $this->name]); $query->andFilterWhere(['like', 'parent.name', $this->parentName]); return $dataProvider; }
/** * Finds the ProductType model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ProductType the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ProductType::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getProductTypeRelation() { return $this->hasOne(ProductType::className(), ['id' => 'type_id']); }
<?php use im\catalog\models\ProductType; use im\eav\widgets\EAVEditor; use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model im\catalog\models\Product */ /* @var $form yii\widgets\ActiveForm */ $attributes = $model->getEAttributes(); if (!$attributes) { $attributes = $model->getAvailableEAttributes(); } ?> <?php echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map(ProductType::find()->asArray()->orderBy('name')->all(), 'id', 'name'), ['prompt' => '']); ?> <?php echo EAVEditor::widget(['attributes' => $attributes, 'form' => $form, 'model' => $model, 'options' => ['id' => 'eav-editor']]); ?> <?php $script = <<<JS var type = \$('[name="Product[type_id]"]'); var editor = \$('#eav-editor').eavEditor(); type.on('change', function() { \$.ajax({ url: '/api/v1/product-types/' + \$(this).val() + '/attributes', dataType: 'json' })
/** * @return \yii\db\ActiveQuery */ public function getChildrenRelation() { return $this->hasMany(ProductType::className(), ['parent_id' => 'id']); }