# 模型类

用户可以在`app/model/`下自定义model模块类，模块类统一使用static方法，控制器中可以通过`mod_{model}::{method}`直接操作，例如：

第一步，我们在`app/model/`目录或者子目录/孙目录下新建一个文件`app/model/mod_team.php`

```
namespace model;

class mod_team extends mod_base
{
    public static $vip_level = 0;

    // 模块类被引入时会先执行_init方法
    public static function _init()
    {
    }

    /**
     * 自定义方法 返回用户人数
     */
    public static function get_total($id)
    {
        // 获取team_id标记为当前team的用户数
        return db::select('COUNT AS `count`')
            ->from('#PB#_user')
            ->where('team_id', $id)
            ->as_field()
            ->execute();
    }
}
```

然后就可以在代码中调用了，例如一个标记团队vip等级的功能，如下：

```
// 获取team数据模型
if (mod_team::get_total($id)) > 100) 
{
    mod_team::vip_level = 1;
}
```

`注意`：类名，文件名，两者者需要保持一致，否则系统会找不到对应的模型。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.kaliphp.com/model.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
