refactor: 处理文章发布时间为空情况

This commit is contained in:
2025-03-26 17:12:49 +08:00
parent d901a288a9
commit 3253820615
3 changed files with 8 additions and 2 deletions

View File

@@ -16,6 +16,12 @@ class ArticleModel extends ArticleBaseModel
// 软删除标记数据字段 // 软删除标记数据字段
protected $deleteTime = 'deleted_at'; protected $deleteTime = 'deleted_at';
// 写入前,处理发布时间为空情况
public static function onBeforeWrite(ArticleModel $article)
{
$article->release_time = empty($article->release_time) ? null : $article->release_time;
}
// 关联分类 // 关联分类
public function category() public function category()
{ {

View File

@@ -25,7 +25,7 @@ class ArticleBaseModel extends BaseModel
'image' => 'string', 'image' => 'string',
'desc' => 'string', 'desc' => 'string',
'recommend' => 'int', 'recommend' => 'int',
'release_time' => 'int', 'release_time' => 'datetime',
'sort' => 'int', 'sort' => 'int',
'link' => 'string', 'link' => 'string',
'content' => 'string', 'content' => 'string',

View File

@@ -45,7 +45,7 @@ class CreateArticle extends Migrator
->addColumn('seo_keywords', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO关键字']) ->addColumn('seo_keywords', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO关键字'])
->addColumn('seo_desc', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO描述']) ->addColumn('seo_desc', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO描述'])
->addColumn('enabled', 'boolean', ['null' => false, 'default' => 1, 'comment' => '是否启用:1是,-1否']) ->addColumn('enabled', 'boolean', ['null' => false, 'default' => 1, 'comment' => '是否启用:1是,-1否'])
->addColumn('release_time', 'datetime', ['null' => false, 'comment' => '发布时间']) ->addColumn('release_time', 'datetime', ['null' => true, 'default' => null, 'comment' => '发布时间'])
->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间']) ->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '更新时间']) ->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '更新时间'])
->addColumn('deleted_at', 'timestamp', ['null' => true, 'comment' => '删除时间']) ->addColumn('deleted_at', 'timestamp', ['null' => true, 'comment' => '删除时间'])