Exemplo n.º 1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Tag::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     $query->andFilterWhere(['like', 'slug', $this->slug]);
     $query->andFilterWhere(['like', 'color', $this->color]);
     if (!empty($this->type)) {
         $query->filterType($this->type);
     }
     return $dataProvider;
 }
 /**
  * @inheritdoc
  */
 public function save($runValidation = true, $attributeNames = null)
 {
     if ($runValidation && $this->validate($attributeNames) === false) {
         return false;
     }
     $trans = Yii::$app->db->beginTransaction();
     try {
         if (parent::save(false, $attributeNames) === false) {
             throw new SaveException($this);
         }
         $this->unlinkAll('tags', true);
         if (!empty($this->tagIds)) {
             foreach ($this->tagIds as $tagId) {
                 $tag = Tag::getOrCreate($tagId);
                 $this->link('tags', $tag);
             }
         }
         $trans->commit();
         return true;
     } catch (\Exception $ex) {
         $trans->rollback();
         throw $ex;
     }
 }
Exemplo n.º 3
0
 /**
  * 
  * @return \jlorente\tagable\db\TagQuery
  */
 public function getTags()
 {
     return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable(Tag::relationTableName(), ['model_id' => 'id'], function ($query) {
         $query->andWhere([Tag::relationTableName() . '.association_type' => $this->getTagAssociationType()]);
     });
 }
 /**
  * Returns the foreign key name of the tag-model relation for tag_id. You can 
  * override this method in order to provide a custom foreign key name.
  * 
  * @return string
  */
 protected function getForeignKeyRelationTag()
 {
     return 'FK_' . Inflector::camelize(Tag::relationTableName()) . '_TagId';
 }
Exemplo n.º 5
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     echo $this->form->field($this->model, 'tagIds')->widget(Select2::className(), ['data' => ArrayHelper::map(Tag::find()->filterType($this->model->getTagAssociationType())->all(), 'id', 'name'), 'options' => ['placeholder' => Yii::t('jlorente/tagable', 'Select a tag')], 'pluginOptions' => ['multiple' => true, 'allowClear' => true, 'tags' => true]]);
 }
 /**
  * Finds the model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * 
  * @param integer $id
  * @return \yii\base\Model the loaded model
  * @throws \yii\web\NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Tag::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 7
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     echo Html::tag('div', Select2::widget(['model' => $this->model, 'attribute' => 'tags', 'data' => ArrayHelper::map(Tag::find()->filterType($this->model->getTagAssociationType())->orderBy('slug ASC')->all(), 'slug', 'name'), 'language' => 'es', 'options' => ['placeholder' => Yii::t('jlorente/tagable', 'Filter by tag') . '...', 'data-url' => $this->url], 'pluginOptions' => ['allowClear' => true, 'multiple' => false]]), ['class' => 'tag-select-filter']);
 }
Exemplo n.º 8
0
 /**
  * Filters the tags by the association type.
  * 
  * @param $type string
  * @return $this
  */
 public function filterType($type)
 {
     return $this->innerJoin(Tag::relationTableName(), Tag::relationTableName() . '.`tag_id` = ' . Tag::tableName() . '.`id`')->andWhere([Tag::relationTableName() . '.`association_type`' => $type])->groupBy([Tag::tableName() . '.`id`']);
 }