Article.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace addons\TinyBlog\common\models;
  3. use common\forms\PostQueryForm;
  4. use common\helpers\StringHelper;
  5. use common\traits\HasOneMerchant;
  6. use common\behaviors\MerchantStoreBehavior;
  7. use common\behaviors\TaggableBehavior;
  8. /**
  9. * This is the model class for table "{{%addon_tiny_blog_article}}".
  10. *
  11. * @property int $id
  12. * @property int|null $merchant_id 商户id
  13. * @property int|null $store_id 店铺id
  14. * @property string $title 标题
  15. * @property string|null $cover 封面
  16. * @property string|null $seo_key seo关键字
  17. * @property string|null $seo_content seo内容
  18. * @property int|null $cate_id 分类id
  19. * @property string|null $description 描述
  20. * @property int $position 推荐位
  21. * @property string|null $content 文章内容
  22. * @property string|null $link 外链
  23. * @property string|null $author 作者
  24. * @property int $view 浏览量
  25. * @property int $sort 优先级
  26. * @property int|null $status 状态
  27. * @property int $created_at 创建时间
  28. * @property int $updated_at 更新时间
  29. */
  30. class Article extends \yii\db\ActiveRecord
  31. {
  32. use MerchantStoreBehavior, HasOneMerchant;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%addon_tiny_blog_article}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['merchant_id', 'store_id', 'cate_id', 'view', 'sort', 'status'], 'integer'],
  47. [['title', 'author', 'cate_id', 'content', 'created_at'], 'required'],
  48. [['content'], 'string'],
  49. [['created_at', 'updated_at', 'position'], 'safe'],
  50. [['title', 'seo_key'], 'string', 'max' => 50],
  51. [['cover'], 'string', 'max' => 255],
  52. [['seo_content'], 'string', 'max' => 1000],
  53. [['description'], 'string', 'max' => 140],
  54. [['link'], 'string', 'max' => 255],
  55. [['author'], 'string', 'max' => 40],
  56. // 标签
  57. [['tagValues'], 'safe'],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'merchant_id' => '商户id',
  68. 'store_id' => '店铺id',
  69. 'title' => '标题',
  70. 'cover' => '封面',
  71. 'seo_key' => 'seo关键字',
  72. 'seo_content' => 'seo内容',
  73. 'cate_id' => '分类',
  74. 'description' => '描述',
  75. 'position' => '推荐位',
  76. 'content' => '文章内容',
  77. 'link' => '原文链接',
  78. 'author' => '作者',
  79. 'view' => '浏览量',
  80. 'sort' => '排序',
  81. 'tagValues' => '标签',
  82. 'status' => '状态',
  83. 'created_at' => '创建时间',
  84. 'updated_at' => '更新时间',
  85. ];
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function transactions()
  91. {
  92. return [
  93. self::SCENARIO_DEFAULT => self::OP_ALL,
  94. ];
  95. }
  96. /**
  97. * @return PostQueryForm
  98. */
  99. public static function find()
  100. {
  101. return new PostQueryForm(get_called_class());
  102. }
  103. /**
  104. * 关联分类
  105. *
  106. * @return \yii\db\ActiveQuery
  107. */
  108. public function getCate()
  109. {
  110. return $this->hasOne(Cate::class, ['id' => 'cate_id']);
  111. }
  112. /**
  113. * 关联标签
  114. *
  115. * @return \yii\db\ActiveQuery
  116. */
  117. public function getTagMap()
  118. {
  119. return $this->hasOne(TagMap::class, ['article_id' => 'id']);
  120. }
  121. /**
  122. * @return \yii\db\ActiveQuery
  123. * @throws \yii\base\InvalidConfigException
  124. */
  125. public function getTags()
  126. {
  127. return $this->hasMany(Tag::class, ['id' => 'tag_id'])
  128. ->viaTable(TagMap::tableName(), ['article_id' => 'id']);
  129. }
  130. /**
  131. * 生成推荐位的值
  132. * @return int|mixed
  133. */
  134. protected function getPosition()
  135. {
  136. $position = $this->position;
  137. $pos = 0;
  138. if (!is_array($position)) {
  139. if ($position > 0) {
  140. return $position;
  141. }
  142. } else {
  143. foreach ($position as $key => $value) {
  144. // 将各个推荐位的值相加
  145. $pos += $value;
  146. }
  147. }
  148. return $pos;
  149. }
  150. /**
  151. * @param bool $insert
  152. * @return bool
  153. */
  154. public function beforeSave($insert)
  155. {
  156. // 推荐位
  157. $this->position = $this->getPosition();
  158. $this->created_at = StringHelper::dateToInt($this->created_at);
  159. $this->updated_at = time();
  160. return parent::beforeSave($insert);
  161. }
  162. /**
  163. * @return \string[][]
  164. */
  165. public function behaviors()
  166. {
  167. return [
  168. 'taggable' => [
  169. 'class' => TaggableBehavior::class,
  170. 'tagValuesAsArray' => true,
  171. 'tagRelation' => 'tags', // 关联表
  172. 'tagAssnRelation' => 'tagMap', // 关联中间表属性
  173. 'tagValueAttribute' => 'title', // 标签字段
  174. 'tagFrequencyAttribute' => 'frequency', // 标签数量字段
  175. ],
  176. ];
  177. }
  178. }
粤ICP备19079148号