public function save() { $group = new PatternGroup(); $group->project_id = \Yii::$app->request->get('project_id'); $group->name = $this->name; if (!$group->save()) { return false; } $patterns = Pattern::find()->where(['group_id' => $this->id])->all(); foreach ($patterns as $pattern) { $patternNew = new Pattern(); $patternNew->attributes = $pattern->attributes; $patternNew->group_id = $group->id; if (!$patternNew->save()) { return false; } $translations = PatternTranslation::find()->where(['pattern_id' => $pattern->id])->all(); foreach ($translations as $translation) { $translationNew = new PatternTranslation(); $translationNew->attributes = $translation->attributes; $translationNew->pattern_id = $patternNew->id; if (!$translationNew->save()) { return false; } } } return true; }
/** * Finds the PatternGroup model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return PatternGroup the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = PatternGroup::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = PatternGroup::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $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, 'priority' => $this->priority, 'project_id' => Yii::$app->request->get('project_id')]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
$this->title = 'Clone Group'; $this->params['breadcrumbs'][] = ['label' => 'Projects', 'url' => ['/project']]; $this->params['breadcrumbs'][] = ['label' => 'Groups', 'url' => ['/group', 'project_id' => Yii::$app->request->get('project_id')]]; $this->params['breadcrumbs'][] = 'Clone'; ?> <div class="pattern-group-create"> <h1><?php echo Html::encode($this->title); ?> </h1> <div class="pattern-group-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'name')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'id')->dropdownList(\common\models\PatternGroup::find()->select(['name', 'id'])->indexBy('id')->column()); ?> <div class="form-group"><?php echo Html::submitButton('Clone', ['class' => 'btn btn-success']); ?> </div> <?php ActiveForm::end(); ?> </div> </div>
use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model common\models\Pattern */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="pattern-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'group_id')->dropdownList(\common\models\PatternGroup::find()->select(['name', 'id'])->indexBy('id')->column(), ['prompt' => 'Group:'])->label('Group'); ?> <?php echo $form->field($model, 'module')->dropDownList(['country' => 'Country', 'city' => 'City', 'direction' => 'Direction', 'airport' => 'Airport', 'airline' => 'Airline'], ['prompt' => '']); ?> <?php echo $form->field($model, 'type_id')->dropdownList(\common\models\PatternType::find()->select(['alias', 'id'])->indexBy('id')->column(), ['prompt' => 'Type:'])->label('Type'); ?> <?php // if ($model && $model->type->is_editor) ?> <?php
/** * @return \yii\db\ActiveQuery */ public function getGroups() { return $this->hasMany(PatternGroup::className(), ['project_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getGroup() { return $this->hasOne(PatternGroup::className(), ['id' => 'group_id']); }