Вот само расширение https://github.com/2amigos/yii2-taggable-behavior Не могу понять эту документацию, только учусь
модель Article:
<?php
namespace app\models;
use Yii;
use app\models\ArticleFile;
use app\models\Category;
use app\models\Tag;
use app\components\ImgBehavior;
use dosamigos\taggable\Taggable;
use app\components\TagBehavior;
use yii\helpers\FileHelper;
/**
* This is the model class for table "article".
*
* @property int $id
* @property string $title
* @property string $desc
* @property string $text
* @property string $date
* @property int $category_id
* @property int $status
*
* @property Comment[] $comments
*/
class Article extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
[
'class' => ImgBehavior::className(),
],
[
'class' => TagBehavior::className(),
],
[
'class' => Taggable::className(),
],
'slug' => [
'class' => 'Zelenin\yii\behaviors\Slug',
'slugAttribute' => 'slug',
'attribute' => 'title',
// optional params
'ensureUnique' => true,
'replacement' => '-',
'lowercase' => true,
'immutable' => false,
// If intl extension is enabled, see http://userguide.icu-project.org/transforms/general.
'transliterateOptions' => 'Russian-Latin/BGN; Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC;'
]
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'article';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['desc', 'text', 'title', 'img', 'img_url'], 'required'],
[['desc', 'text', 'slug'], 'string'],
[['date', 'sup_hot', 'imageFile', 'slug', 'tagNames'], 'safe'],
[['category_id', 'status'], 'integer'],
[['title'], 'string', 'max' => 255],
[['slug'], 'unique', 'targetClass' => 'app\models\Article', 'targetAttribute' => 'slug'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'slug' => 'Slug',
'desc' => 'Desc',
'text' => 'Text',
'date' => 'Date',
'category_id' => 'Category ID',
'status' => 'Status',
];
}
public function upload($slug, $date)
{
$url = 'file/article/'.date("Y").'/'.date("m");
FileHelper::createDirectory($url, $mode = 0775, $recursive = true);
if ($this->validate()) {
$this->imageFile->saveAs($url .'/'. md5($slug.$date). '.' . $this->imageFile->extension);
return true;
} else {
return false;
}
}
public function newImage($id, $ext)
{
$url = '/2018/img/'.date("my").'/';
$article = Article::find()
->where(['id' => $id])
->one();
$article->img = $url.md5($article->slug.$article->date).'.'.$ext;
if ($article->save()) {
return true;
} else {
return false;
}
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('tag_article', ['article_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTag()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('tag_article', ['article_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getComments()
{
return $this->hasMany(Comment::className(), ['article_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getArticleFile()
{
return $this->hasMany(ArticleFile::className(), ['article_id' => 'id']);
}
}
Модель Tag:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "tag".
*
* @property int $id
* @property string $title
*
* @property ArticleTag[] $articleTags
*/
class Tag extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'tag';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['title'], 'required'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getArticleTags()
{
return $this->hasMany(ArticleTag::className(), ['tag_id' => 'id']);
}
}
Модель TagArticle
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "tag_article".
*
* @property int $id
* @property int $article_id
* @property int $tag_id
*/
class TagArticle extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'tag_article';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['article_id', 'tag_id'], 'required'],
[['article_id', 'tag_id'], 'integer'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'article_id' => 'Article ID',
'tag_id' => 'Tag ID',
];
}
}
Нужно было просто подключить use yii\web\Response;
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
При проверке условия один из сайтов где подходит совпадение по строке распостроняет ответ echo не корректно отрабатываетприписывает ответ...
Хочу генерировать на сайте несколько вариантов js (es5, es2015, es2016), и отдавать клиенту нужный файл с js, в зависимости от того, какой стандарт поддерживает...
Пытаюсь на сайт добавить "бесконечный скролинг": необходимо чтобы при прокрутке страницы вниз загружались новые статьи (Ajax)Всё настроил,...