<?php
declare (strict_types = 1);

namespace app\openapi\model;

use app\common\model\ArticleCategoryBaseModel;
use think\facade\Db;

/**
 * 文章分类模型
 * @mixin \think\Model
 */
class ArticleCategoryModel extends ArticleCategoryBaseModel
{
    // 所属语言范围查询
    public function scopeLanguage($query, $language = 'zh-cn')
    {
        $query->whereExists(function($subquery) use($language) {
            $lang_model = new LanguageModel;
            $subquery->model($lang_model)
                ->name($lang_model->getName())
                ->field(['id'])
                ->where('id', '=', Db::raw($this->getTable() . '.language_id'))
                ->where('code', '=', $language);
        });
    }

    // 所属上级分类范围查询
    public function scopeParent($query, $parent_id)
    {
        if (is_null($parent_id)) return;
        $query->where('pid', '=', $parent_id);
    }
}
