94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
/*
|
|
* @Author: YwxApp <ywx@ywxapp.cn>
|
|
* @Date: 2026-08-10 15:28:14
|
|
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
|
* @LastEditTime: 2026-08-11 00:39:57
|
|
* @Description:
|
|
* @FilePath: \ywxapp_dev\addon\articles\model\ArticleCategory.php
|
|
* @CustomString: Copyright (c) 2026 YwxApp
|
|
*/
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | 文章分类模型
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types = 1);
|
|
|
|
namespace addon\articles\model;
|
|
|
|
use think\model\concern\SoftDelete;
|
|
use ywxapp\model\BaseModel;
|
|
|
|
class ArticleCategory extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
|
|
protected $name = 'articles_category';
|
|
protected $deleteTime = 'delete_at';
|
|
protected $defaultSoftDelete = 0;
|
|
|
|
protected $autoWriteTimestamp = true;
|
|
protected $createTime = 'create_at';
|
|
protected $updateTime = 'update_at';
|
|
|
|
protected $type = [
|
|
'sort' => 'integer',
|
|
'status' => 'integer',
|
|
];
|
|
|
|
public function articles()
|
|
{
|
|
return $this->hasMany(Article::class, 'cid', 'id');
|
|
}
|
|
|
|
public static function allEnable()
|
|
{
|
|
return self::where('delete_at', 0)
|
|
->where('status', 1)
|
|
->order('sort', 'asc')
|
|
->order('id', 'asc')
|
|
->select();
|
|
}
|
|
|
|
public static function getByAlias(string $alias)
|
|
{
|
|
return self::where('delete_at', 0)->where('alias', $alias)->find();
|
|
}
|
|
|
|
public static function getById(int $id)
|
|
{
|
|
return self::where('delete_at', 0)->find($id);
|
|
}
|
|
|
|
public static function hasArticles(int $id): bool
|
|
{
|
|
return Article::where('delete_at', 0)->where('cid', $id)->count() > 0;
|
|
}
|
|
|
|
public static function ensureSchema(): void
|
|
{
|
|
$prefix = self::currentPrefix();
|
|
self::ensureTableFromInstall($prefix, 'articles_category');
|
|
}
|
|
|
|
/**
|
|
* 兼容旧框架 ArticleCategory::tabTree 的树形展开输出
|
|
* 插件分类为扁平结构(无 parent_id),此处直接按 sort/id 顺序输出带层级前缀的列表。
|
|
* @param iterable $arr 分类数据集
|
|
* @param int $pid 兼容参数,保留原签名
|
|
* @param int $lv 兼容参数,保留原签名
|
|
*/
|
|
public static function tabTree($arr, $pid = 0, $lv = 0)
|
|
{
|
|
$result = [];
|
|
foreach ($arr as $item) {
|
|
$data = is_array($item) ? $item : $item->toArray();
|
|
$data['lv'] = $lv + 1;
|
|
$data['title'] = str_repeat('|——', $lv + 1) . ($data['name'] ?? ($data['title'] ?? ''));
|
|
$result[] = $data;
|
|
}
|
|
return $result;
|
|
}
|
|
}
|