chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\model;
|
||||
|
||||
use ywxapp\model\BaseModel;
|
||||
|
||||
|
||||
class BlogCategory extends BaseModel
|
||||
{
|
||||
|
||||
// 设置数据表名
|
||||
protected $name = 'blog_category';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
protected $createTime = 'create_at';
|
||||
protected $updateTime = 'update_at';
|
||||
|
||||
// 定义允许写入的字段
|
||||
protected $allowField = [
|
||||
'uid', 'pid', 'name', 'description', 'sort', 'status'
|
||||
];
|
||||
|
||||
// 设置字段类型
|
||||
protected $type = [
|
||||
'uid' => 'int',
|
||||
'pid' => 'int',
|
||||
'sort' => 'int',
|
||||
'status' => 'int',
|
||||
'create_at' => 'int',
|
||||
'update_at' => 'int'
|
||||
];
|
||||
|
||||
// 定义关联 - 文章
|
||||
|
||||
public function articles()
|
||||
{
|
||||
return $this->hasMany(BlogArticle::class, 'cid');
|
||||
}
|
||||
|
||||
// 定义关联 - 父级分类
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(self::class, 'pid');
|
||||
}
|
||||
|
||||
// 定义关联 - 子分类
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(self::class, 'pid');
|
||||
}
|
||||
|
||||
// 获取文章数量
|
||||
|
||||
public function getArticleCount()
|
||||
{
|
||||
return $this->articles()->where('status', 1)->count();
|
||||
}
|
||||
|
||||
// 检查分类是否可用
|
||||
|
||||
public function isActive()
|
||||
{
|
||||
return $this->status === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某会员「可用」的分类:自己创建的(uid=$uid) + 全局共享的(uid=0)
|
||||
* 用于会员中心写文章时的分类下拉。
|
||||
*/
|
||||
public static function getAvailable(int $uid)
|
||||
{
|
||||
return self::where('status', 1)
|
||||
->where(function ($query) use ($uid) {
|
||||
$query->where('uid', 0)->whereOr('uid', $uid);
|
||||
})
|
||||
->order('sort', 'asc')
|
||||
->select();
|
||||
}
|
||||
|
||||
// 获取分类树形结构
|
||||
|
||||
public static function getCategoryTree()
|
||||
{
|
||||
$categories = self::order('sort', 'asc')->select()->toArray();
|
||||
return self::buildTree($categories);
|
||||
}
|
||||
|
||||
// 递归构建树形结构
|
||||
|
||||
private static function buildTree($items, $parentId = 0)
|
||||
{
|
||||
$tree = [];
|
||||
foreach ($items as $item) {
|
||||
if (($item['pid'] ?? 0) == $parentId) {
|
||||
$children = self::buildTree($items, $item['id']);
|
||||
if ($children) {
|
||||
$item['children'] = $children;
|
||||
}
|
||||
$tree[] = $item;
|
||||
}
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user