luck-draw/app/model/Archives.php

91 lines
2.2 KiB
PHP

<?php
namespace app\model;
use app\exception\RepositoryException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasMany;
use think\model\relation\HasOne;
class Archives extends Base
{
public const ORIGINAL_TABLE = 'bee_archives';
public const STATUS_NORMAL = 1;//正常
public const STATUS_DISABLE = 0;//禁用
//根据栏目ID获取文章分页列表
public static function getListPageByCategory($categoryId, $per = 20, $keyword = '')
{
$where = [
['category_id', '=', $categoryId],
];
$param = [];
//$param['category_id'] = $categoryId;
if ($keyword != '') {
$where[] = ['title', 'like', '%' . $keyword . '%'];
$param['keyword'] = $keyword;
}
$paginate = [
'list_rows' => $per,
'query' => $param
];
return self::with(["archivesCategory"])->where($where)
->order(["sort"=>"desc"])
->paginate($paginate, false);
}
/**
* 创建人信息
*
* @return HasOne
*/
public function member(): HasOne
{
return $this->hasOne(Member::class, 'id', 'created_by')->bind(['nickname']);
}
/**
* 栏目 后台用
*
* @return HasOne
*/
public function category(): HasOne
{
return $this->hasOne(ArchivesCategory::class, 'id', 'category_id')->bind(['category' => 'title']);
}
/**
* 栏目
* @return HasOne
*/
public function archivesCategory(): HasOne
{
return $this->hasOne(ArchivesCategory::class, 'id', 'category_id');
}
public static function onAfterInsert( $archives)
{
$archives->sort = ceil(self::max("sort")+1);
$archives->save();
}
public static function onBeforeUpdate( $archives)
{
//检查sort
$hasFlag = self::where([
["sort","=",$archives->sort],
["id","<>",$archives->id]
])->find();
if(!empty($hasFlag)){
throw new RepositoryException('sort不能重复,保留小数点后2位');
}
}
}