/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = EavEntityModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'entity_name', $this->entity_name])->andFilterWhere(['like', 'entity_model', $this->entity_model]); return $dataProvider; }
protected function loadEavAttributes() { if (!$this->isAttributesLoaded) { $modelTable = EavEntityModel::tableName(); $entityTable = EavEntity::tableName(); $attributeTable = EavAttribute::tableName(); $entityToAttributeTable = '{{%eav_entity_attribute}}'; $attributes = EavAttribute::find()->innerJoin($entityToAttributeTable, "{$attributeTable}.id = {$entityToAttributeTable}.attribute_id")->innerJoin($entityTable, "{$entityToAttributeTable}.entity_id = {$entityTable}.id")->innerJoin($modelTable, "{$entityTable}.model_id = {$modelTable}.id")->andWhere(["{$modelTable}.entity_model" => $this->ownerClassName])->andWhere(["{$entityTable}.category_id" => $this->getEavCatgoryId()])->orderBy(['order' => SORT_DESC])->all(); $validators = $this->owner->getValidators(); foreach ($attributes as $attribute) { $this->eavAttributes->{$attribute['name']} = $attribute; $validators[] = \yii\validators\Validator::createValidator('safe', $this->owner, [$attribute['name']], []); } $this->isAttributesLoaded = true; } }
public function setValue($entityModel, $catgoryId, $itemId, $value) { if (!isset($this->_value) || !$this->_value) { $this->_value = $this->loadValue($entityModel, $catgoryId, $itemId); } if (!$this->_value) { if (!($eavModel = EavEntityModel::findOne(['entity_model' => $entityModel]))) { throw new InvalidParamException(Yii::t('yee/eav', 'Model was not found.')); } if (!($eavEntity = EavEntity::findOne(['model_id' => $eavModel->id, 'category_id' => $catgoryId]))) { throw new InvalidParamException(Yii::t('yee/eav', 'Entity was not found.')); } $eavValue = new EavValue(['entity_id' => $eavEntity->id, 'attribute_id' => $this->getPrimaryKey(), 'item_id' => $itemId, 'value' => $value]); if (!$eavValue->save()) { throw new \yii\db\Exception(Yii::t('yee/eav', 'An error occurred during creation of EavValue record.')); } $this->_value = $eavValue; } $this->_value->value = $value; }
public function actionGetCategories() { Yii::$app->response->format = Response::FORMAT_JSON; $model_id = (int) Yii::$app->getRequest()->post('model_id'); $model = EavEntityModel::findOne($model_id); if ($model) { try { $model = new $model->entity_model(); if ($model instanceof EavCategories) { $categories = $model->getEavCategories(); if ($categories && !empty($categories)) { $categories = ArrayHelper::merge(['' => Yii::t('yee', 'Not Selected')], $categories); $dropDown = Html::dropDownList('entityCategory', null, $categories, ['id' => 'entityCategory', 'class' => 'form-control']); return ['list' => $dropDown]; } } } catch (Exception $exc) { return null; } } return null; }