// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\docs\model; use think\Model; /** * 文档正文模型 * * @author ywxapp */ class DocsDoc extends Model { protected $name = 'docs_doc'; protected $autoWriteTimestamp = true; protected $createTime = 'create_at'; protected $updateTime = 'update_at'; protected $type = [ 'id' => 'integer', 'project_id' => 'integer', 'version_id' => 'integer', 'pid' => 'integer', 'editor_type' => 'integer', 'is_dir' => 'integer', 'views' => 'integer', 'uid' => 'integer', 'editor_uid' => 'integer', 'audit_status' => 'integer', 'sort' => 'integer', 'status' => 'integer', ]; /** * 关联所属项目 */ public function project() { return $this->belongsTo(DocsProject::class, 'project_id', 'id'); } /** * 关联所属版本 */ public function version() { return $this->belongsTo(DocsVersion::class, 'version_id', 'id'); } /** * 按项目 + 版本 + 标识查找已发布文档 * * @param int $projectId 项目ID * @param int $versionId 版本ID * @param string $name 文档标识 * @return static|null */ public static function findByName(int $projectId, int $versionId, string $name) { if ($name === '') { return null; } return static::where('project_id', $projectId) ->where('version_id', $versionId) ->where('name', $name) ->where('status', 1) ->find(); } /** * 阅读量自增 * * @param int $id 文档ID * @return void */ public static function addViews(int $id): void { if ($id > 0) { static::where('id', $id)->inc('views')->update(); } } }