public function actionEdit($id = null) { $model = new Form(); if ($id > 0) { $model = Form::findOne($id); } /** @var \app\models\Object $object */ $object = Object::getForClass(Form::className()); $propIds = (new Query())->select('property_group_id')->from([ObjectPropertyGroup::tableName()])->where(['and', 'object_id = :object', 'object_model_id = :id'], [':object' => $object->id, ':id' => $id])->column(); $post = \Yii::$app->request->post(); $properties = isset($post['Form']['properties']) ? $post['Form']['properties'] : []; if ($model->load($post) && $model->validate()) { if ($model->save()) { $id = $model->id; $remove = []; $add = []; foreach ($propIds as $value) { $key = array_search($value, $properties); if ($key === false) { $remove[] = $value; } else { unset($properties[$key]); } } foreach ($properties as $value) { $add[] = [$value, $object->id, $id]; } Yii::$app->db->createCommand()->delete(ObjectPropertyGroup::tableName(), ['and', 'object_id = :object', 'object_model_id = :id', ['in', 'property_group_id', $remove]], [':object' => $object->id, ':id' => $id])->execute(); if (!empty($add)) { Yii::$app->db->createCommand()->batchInsert(ObjectPropertyGroup::tableName(), ['property_group_id', 'object_id', 'object_model_id'], $add)->execute(); } \Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); $returnUrl = Yii::$app->request->get('returnUrl', ['/backend/form/index', 'id' => $model->id]); switch (Yii::$app->request->post('action', 'save')) { case 'next': return $this->redirect(['/backend/form/edit', 'returnUrl' => $returnUrl]); case 'back': return $this->redirect($returnUrl); default: return $this->redirect(Url::toRoute(['/backend/form/edit', 'id' => $model->id, 'returnUrl' => $returnUrl])); } } else { \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); } } $items = ArrayHelper::map(PropertyGroup::find()->where('object_id = :object', [':object' => $object->id])->all(), 'id', 'name'); return $this->render('edit', ['model' => $model, 'items' => $items, 'selected' => $propIds]); }
public function up() { $objects = \app\models\Object::find()->asArray()->all(); foreach ($objects as $object) { if ($object['name'] == 'Submission') { continue; } else { if ($object['name'] == 'Form') { $submissionObject = \app\models\Object::findOne(['name' => 'Submission']); $this->addColumn($object['eav_table_name'], 'property_group_id', 'INT UNSIGNED NOT NULL AFTER `object_model_id`'); $this->addColumn($submissionObject->eav_table_name, 'property_group_id', 'INT UNSIGNED NOT NULL AFTER `object_model_id`'); $groups = \app\models\PropertyGroup::find()->where(['object_id' => $object['id']])->asArray()->all(); foreach ($groups as $group) { $forms = \app\models\ObjectPropertyGroup::find()->select('object_model_id')->where(['property_group_id' => $group['id']])->asArray()->all(); $formIDs = []; foreach ($forms as $formID) { if (!in_array($formID['object_model_id'], $formIDs)) { $formIDs[] = $formID['object_model_id']; } } $submissionIDs = \app\models\Submission::find()->select('id')->where(['form_id' => $formIDs])->asArray()->all(); $subIDs = []; foreach ($submissionIDs as $submission) { $subIDs[] = $submission['id']; } $properties = \app\models\Property::find()->select(['id', 'key'])->where(['property_group_id' => $group['id'], 'is_eav' => 1])->asArray()->all(); foreach ($properties as $property) { $this->update($submissionObject->eav_table_name, ['property_group_id' => $group['id']], ['key' => $property['key']]); } } } else { $this->addColumn($object['eav_table_name'], 'property_group_id', 'INT UNSIGNED NOT NULL AFTER `object_model_id`'); $groups = \app\models\PropertyGroup::find()->where(['object_id' => $object['id']])->asArray()->all(); foreach ($groups as $group) { $properties = \app\models\Property::find()->select(['id', 'key'])->where(['property_group_id' => $group['id'], 'is_eav' => 1])->asArray()->all(); foreach ($properties as $property) { $this->update($object['eav_table_name'], ['property_group_id' => $group['id']], ['key' => $property['key']]); } } } } } }
public function actionRemoveAllGroups() { $items = Yii::$app->request->post('items', []); if (!empty($items)) { $items = PropertyGroup::find()->where(['in', 'id', $items])->all(); foreach ($items as $item) { $properties = Property::find()->where(['property_group_id' => $item->id])->all(); foreach ($properties as $prop) { $prop->delete(); } $item->delete(); } } return $this->redirect(['index']); }
/** * Returns array of ids of propertyGroups for user model * @return array * @throws \Exception */ private function getPropertyIdsForUsers() { return PropertyGroup::getDb()->cache(function ($db) { return PropertyGroup::find()->select('id')->where(['object_id' => $this->getObject()->id])->asArray()->column($db); }, 86400, new TagDependency(['tags' => ActiveRecordHelper::getCommonTag(PropertyGroup::className())])); }