/** @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 search($params) { $query = self::find(); $dataProvider = new \yii\data\ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 20], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $dataProvider->sort->attributes['userName'] = ['asc' => ['user_id' => SORT_ASC], 'desc' => ['user_id' => SORT_DESC]]; $dataProvider->sort->attributes['postTitle'] = ['asc' => ['post_id' => SORT_ASC], 'desc' => ['post_id' => SORT_DESC]]; $query->joinWith(['post', 'user']); if ($this->load($params) && $this->validate()) { $query->andFilterWhere(['post_id' => $this->post_id]); $query->andFilterWhere(['like', Post::tableName() . '.title', $this->postTitle]); if (!is_null($this->userName) && $this->userName != '') { $userName[] = explode(' ', $this->userName); foreach ($userName as $item) { $query->andFilterWhere(['or', ['like', Profile::tableName() . '.name', $item]]); } } if (($time = strtotime($this->created_at)) > 0) { $query->andFilterWhere(['>', 'created_at', $time]); $query->andFilterWhere(['<', 'created_at', $time + 24 * 60 * 60]); } } return $dataProvider; }
public function getPost() { return $this->hasOne(Post::className(), ['id' => 'post_id']); }