public function init() { parent::init(); if ($this->item instanceof Tag) { $this->id = $this->item->id; $this->setAttributes($this->item->getAttributes()); $this->setIsNewRecord($this->item->getIsNewRecord()); } }
/** @inheritdoc */ public function bootstrap($app) { /** @var Module $module */ /** @var \yii\db\ActiveRecord $modelName */ if ($app->hasModule('blog') && ($module = $app->getModule('blog')) instanceof Module) { Yii::$container->setSingleton(BlogFinder::className(), ['tagQuery' => \jarrus90\Blog\Models\Tag::find(), 'postQuery' => \jarrus90\Blog\Models\Post::find(), 'commentQuery' => \jarrus90\Blog\Models\Comment::find()]); if (!$app instanceof ConsoleApplication) { $module->controllerNamespace = 'jarrus90\\Blog\\Controllers'; $configUrlRule = ['prefix' => $module->urlPrefix, 'rules' => $module->urlRules]; if ($module->urlPrefix != 'blog') { $configUrlRule['routePrefix'] = 'blog'; } $configUrlRule['class'] = 'yii\\web\\GroupUrlRule'; $rule = Yii::createObject($configUrlRule); $app->urlManager->addRules([$rule], false); } else { if (empty($app->controllerMap['migrate'])) { $app->controllerMap['migrate']['class'] = 'yii\\console\\controllers\\MigrateController'; } $app->controllerMap['migrate']['migrationNamespaces'][] = 'jarrus90\\Blog\\migrations'; } if (!isset($app->get('i18n')->translations['blog*'])) { $app->get('i18n')->translations['blog*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } } }
/** * Validation rules * @return array */ public function rules() { $rules = $this->_model->rules(); $rules[] = ['tags', 'exist', 'targetClass' => Tag::className(), 'targetAttribute' => 'id', 'allowArray' => true]; $rules['keyExists'] = ['key', 'unique', 'targetClass' => Post::className(), 'targetAttribute' => 'key', 'when' => function ($model) { return $model->key != $model->item->key; }]; return $rules; }
public function getTags() { return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->via('tagRelation'); }
</div> </div> <div class="box box-default"> <div class="box-header with-border"> <?php echo Yii::t('blog', 'Tags'); ?> <div class="box-tools pull-right"> <button type="button" class="btn btn-box-tool" data-widget="collapse"> <i class="fa fa-minus"></i> </button> </div> </div> <div class="box-body"> <?php echo $form->field($model, 'tags', ['template' => '{input}{hint}{error}'])->widget(Select2::className(), ['data' => ArrayHelper::map(Tag::find()->orderBy(['title' => SORT_ASC])->asArray()->all(), 'id', 'title'), 'options' => ['placeholder' => Yii::t('blog', 'Select tags'), 'multiple' => true], 'theme' => 'default']); ?> </div> </div> </div> </div> <div class="row"> <div class="col-md-9"> <?php echo Html::submitButton(Yii::t('blog', 'Save'), ['class' => 'btn btn-success btn-block']); ?> </div> </div> <?php ActiveForm::end(); ?>