1
This commit is contained in:
29
app/common/model/ArticleCates.php
Normal file
29
app/common/model/ArticleCates.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\model\relation\HasMany;
|
||||
|
||||
class ArticleCates extends TimeModel
|
||||
{
|
||||
protected array $rule = [
|
||||
'title' => 'require|max:50|unique:article_cates',
|
||||
'sort' => 'integer|egt:0',
|
||||
'status' => 'in:0,1'
|
||||
];
|
||||
|
||||
protected array $message = [
|
||||
'title.require' => '分类名称不能为空',
|
||||
'title.max' => '分类名称最多50个字符',
|
||||
'title.unique' => '分类名称已存在',
|
||||
'sort.integer' => '排序必须为整数',
|
||||
'sort.egt' => '排序不能小于0',
|
||||
'status.in' => '状态值错误'
|
||||
];
|
||||
|
||||
public function articles(): HasMany
|
||||
{
|
||||
return $this->hasMany(Articles::class, 'cate_id');
|
||||
}
|
||||
|
||||
}
|
||||
40
app/common/model/Articles.php
Normal file
40
app/common/model/Articles.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
class Articles extends TimeModel
|
||||
{
|
||||
protected array $rule = [
|
||||
'title' => 'require|max:50',
|
||||
'cate_id' => 'require|integer|gt:0',
|
||||
'cover' => 'max:255',
|
||||
'summary' => 'max:255',
|
||||
'author' => 'max:50',
|
||||
'sort' => 'integer|egt:0',
|
||||
'status' => 'in:0,1'
|
||||
];
|
||||
protected array $message = [
|
||||
'title.require' => '文章标题不能为空',
|
||||
'title.max' => '文章标题最多50个字符',
|
||||
'cate_id.require' => '分类ID不能为空',
|
||||
'cate_id.integer' => '分类ID必须为整数',
|
||||
'cate_id.gt' => '分类ID必须大于0',
|
||||
'cover.max' => '封面路径最多255个字符',
|
||||
'summary.max' => '简介最多255个字符',
|
||||
'author.max' => '作者最多50个字符',
|
||||
'sort.integer' => '排序必须为整数',
|
||||
'sort.egt' => '排序不能小于0',
|
||||
'status.in' => '状态值错误'
|
||||
];
|
||||
|
||||
public function cate()
|
||||
{
|
||||
return $this->belongsTo(ArticleCates::class, 'cate_id');
|
||||
}
|
||||
|
||||
|
||||
public function getCateNameAttr($value, $data)
|
||||
{
|
||||
return $this->cate ? $this->cate->title : '';
|
||||
}
|
||||
}
|
||||
42
app/common/model/TimeModel.php
Normal file
42
app/common/model/TimeModel.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 有关时间的模型
|
||||
* Class TimeModel
|
||||
* @package app\common\model
|
||||
*/
|
||||
class TimeModel extends Model
|
||||
{
|
||||
|
||||
public array $statusText = [
|
||||
1 => '启用',
|
||||
-1 => '禁用',
|
||||
0 => '未启用',
|
||||
];
|
||||
/**
|
||||
* 软删除
|
||||
*/
|
||||
use SoftDelete;
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'autoWriteTimestamp' => true,
|
||||
'createTime' => 'create_time',
|
||||
'updateTime' => 'update_time',
|
||||
'deleteTime' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusTextAttr($value): string
|
||||
{
|
||||
return $this->statusText[$value] ?? '未知状态';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user