/** * @return \yii\db\ActiveQuery */ public function getCategory() { return $this->hasOne(Category::className(), ['id' => 'category_id']); }
<?php use yii\helpers\Html; use yii\helpers\HtmlPurifier; use yii\widgets\ActiveForm; use yii\widgets\DetailView; use amilna\blog\models\Category; /* @var $this yii\web\View */ /* @var $model amilna\blog\models\Post */ $this->title = $model->title; $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Posts'), 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; $this->registerMetaTag(['name' => 'title', 'content' => Html::encode($model->title)]); $this->registerMetaTag(['name' => 'description', 'content' => Html::encode($model->description)]); $cat = new Category(); ?> <div class="post-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <div class="row"> <!-- Blog Post --> <div class="col-sm-8 panel"> <div class="panel-body"> <div> <h3><?php echo Html::encode($model->author ? $model->author->username : ""); ?>
use kartik\widgets\SwitchInput; use kartik\datetime\DateTimePicker; use amilna\blog\models\Category; use iutbay\yii2kcfinder\KCFinderInputWidget; $module = Yii::$app->getModule('blog'); if ($module->enableUpload) { // kcfinder options // http://kcfinder.sunhater.com/install#dynamic $kcfOptions = array_merge([], ['uploadURL' => Yii::getAlias($module->uploadURL), 'uploadDir' => Yii::getAlias($module->uploadDir), 'access' => ['files' => ['upload' => true, 'delete' => false, 'copy' => false, 'move' => false, 'rename' => false], 'dirs' => ['create' => true, 'delete' => false, 'rename' => false]], 'types' => ['files' => "", 'images' => "*img"], 'thumbWidth' => 260, 'thumbHeight' => 260]); // Set kcfinder session options Yii::$app->session->set('KCFINDER', $kcfOptions); } /* @var $this yii\web\View */ /* @var $model amilna\blog\models\Post */ /* @var $form yii\widgets\ActiveForm */ $cat = new Category(); $listCategory = [] + ArrayHelper::map($cat->parents(), 'id', 'title'); $category = []; foreach ($model->blogCatPos as $c) { array_push($category, $c->category_id); } ?> <div class="post-form"> <?php $form = ActiveForm::begin(); ?> <div class="row"> <div class="col-md-9"> <div class="row">
/** * @return \yii\db\ActiveQuery */ public function getCategories() { return $this->hasMany(Category::className(), ['parent_id' => 'id']); }
/** * Finds the Category model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Category the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Category::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function find() { return parent::find()->where([Category::tableName() . '.isdel' => 0]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['author']); $dataProvider = new ActiveDataProvider(['query' => $query]); $userClass = Yii::$app->getModule('blog')->userClass; /* uncomment to sort by relations table on respective column $dataProvider->sort->attributes['blogcatposId'] = [ 'asc' => ['{{%blogcatpos}}.id' => SORT_ASC], 'desc' => ['{{%blogcatpos}}.id' => SORT_DESC], ];*/ $dataProvider->sort->attributes['authorName'] = ['asc' => [$userClass::tableName() . '.username' => SORT_ASC], 'desc' => [$userClass::tableName() . '.username' => SORT_DESC]]; $dataProvider->sort->attributes['term'] = ['asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['status' => $this->status, 'isdel' => $this->isdel, 'isfeatured' => $this->isfeatured]); $params = self::queryNumber([['id', $this->tableName()], ['author_id']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['title'], ['description'], ['content'], ['tags'], ['image']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time']]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(['like', 'lower(' . $userClass::tableName() . '.username)', strtolower($this->authorName)]); if ($this->category || $this->term) { $term = $this->term ? $this->term : $this->category; $dsn = $this->db->dsn; $cquery = $this->find(); if (strtolower(substr($dsn, 0, 5)) == "mysql") { $cquery->select(["GROUP_CONCAT(" . $this->tableName() . ".id)"]); } else { $cquery->select(["array_agg(" . $this->tableName() . ".id)"]); } $cquery->leftJoin(BlogCatPos::tableName() . " as cp", $this->tableName() . ".id = cp.post_id")->leftJoin(Category::tableName() . " as c", "cp.category_id = c.id"); if ($this->category) { $cquery->andWhere("lower(c.title) = '" . strtolower($term) . "'"); } else { $cquery->andWhere("lower(c.title) like '%" . strtolower($term) . "%' or lower(c.description) like '%" . strtolower($term) . "%'"); } $res = $cquery->scalar(); $res = $res == "" ? "{}" : $res; if ($this->category) { //$query->andFilterWhere(["OR","false",$this->tableName().".id = ANY ('".$res."')"]); $query->andFilterWhere(["OR", "false", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]); } else { $query->andFilterWhere(["OR", "lower(title) like '%" . strtolower($this->term) . "%'", ["OR", "lower(description) like '%" . strtolower($this->term) . "%'", ["OR", "lower(tags) like '%" . strtolower($this->term) . "%'", ["OR", "lower(content) like '%" . strtolower($this->term) . "%'", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]]]]); } } return $dataProvider; }