public function init() { parent::init(); if ($this->item instanceof Comment) { $this->id = $this->item->id; $this->setAttributes($this->item->getAttributes(), false); $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']; } } }
public function attributeLabels() { $labels = parent::attributeLabels(); $labels['postTitle'] = Yii::t('blog', 'Post'); $labels['userName'] = Yii::t('blog', 'User'); return $labels; }
public function actionPost($key) { $post = $this->getPost($key); $commentForm = null; if ($post->comments_enabled) { $item = Yii::createObject(['class' => Comment::className()]); $commentForm = Yii::createObject(['class' => Comment::className(), 'scenario' => 'create', 'item' => $item]); $commentForm->setAttributes(['created_by' => Yii::$app->user->id, 'post_id' => $post->id], false); $this->performAjaxValidation($commentForm); if ($commentForm->load(Yii::$app->request->post()) && $commentForm->save()) { return $this->refresh(); } } return $this->render('post', ['post' => $post, 'commentForm' => $commentForm]); }
public function getComments() { return $this->hasMany(Comment::className(), ['post_id' => 'id']); }