入口:XR_M() / XR_M('table') DDL:Table 门面 → 驱动 DML:Model 链式
XR_M() 是 \Phpcmf\Service::M() 的简写。
| 方法 | 参数 | 说明 |
|---|---|---|
table | $name 表名(一般不含前缀,框架会按配置加前缀) | 后续 CRUD 作用于此表 |
table_site | $name 站点表短名;$site 站点 ID,默认当前站 | 自动加站点表前缀 |
dbprefix | $name 可选短表名 | 返回带前缀的完整表名 |
DDL 方法(create_table / add_field 等)的 $table 参数一般为已含前缀的完整表名,推荐先用 dbprefix() 生成。
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 短表名或完整表名;若未带前缀会自动 dbprefix |
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 短表名或完整表名 |
$name | string | 字段名 |
禁止再用 SHOW TABLES LIKE / SHOW COLUMNS 手写判断,统一走以上方法(内部为驱动 tableExists / fieldExists)。
通过 XR_M('table') 调用,底层由数据库驱动实现(当前 MySQLi:CREATE TABLE IF NOT EXISTS)。
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 完整表名(建议 $this->dbprefix('xxx')) |
$fields | array | 字段定义,两种写法:
|
$indexs | array | 索引片段,如 ['PRIMARY KEY (`id`)', 'KEY `uid` (`uid`)'] |
$note | string | 表注释(COMMENT) |
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 完整表名 |
$ifExists | bool | 默认 true,生成 DROP TABLE IF EXISTS |
由驱动返回当前表的 CREATE 语句,并解析出字段片段,供克隆附表等场景使用。
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 完整表名(可含前缀) |
$name | string | 字段名 |
$type | string | 字段类型,如 VARCHAR(50)、INT(10) |
$info | string | 附加属性,如 NOT NULL、DEFAULT NULL、NOT NULL DEFAULT 0 |
$note | string | 字段注释 |
参数同 add_field。对应驱动 ALTER TABLE ... CHANGE/MODIFY(由 sqlEditField 生成)。
| 参数 | 类型 | 说明 |
|---|---|---|
$table | string | 完整表名 |
$name | string | 字段名 |
$table 为短表名(内部会 dbprefix);$config 为字段配置数组。已存在字段会跳过;SQL 由字段类 create_sql → 驱动 sqlAddField 生成。
不要再直接拼 ALTER TABLE ... ADD/CHANGE/DROP。自定义字段类型应实现 sqlAddField / sqlEditField / sqlDropField 相关路径。
用于系统安装:按数组定义建表并写入种子数据。配置文件:Fcms/Config/Install.php(type => cms_schema)。
| 键 | 说明 |
|---|---|
tables | [短表名 => ['fields'=>..., 'indexes'=>..., 'comment'=>'', 'drop'=>true]]drop=true 时先 drop_table 再 create_table |
seeds | [['table'=>'短表名', 'data'=>关联数组], ...],内部用 replace() 写入 |
在 get / getAll / getRow / update / delete 等执行前叠加条件。一次查询结束后内部会 _clear()。
| 方法 | 签名 | 说明 |
|---|---|---|
| select | select($field) | 指定查询字段,可多次调用 |
| where | where($name, $value = '') | $name 可为字段名或数组 ['a'=>1,'b'=>2];仅传 SQL 片段时不传 value |
| where_in | where_in($name, $value) | $value 为数组 |
| like | like($name, $value) | 模糊匹配(优先驱动 whereLike) |
| where_date | where_date($name, $value) | 按日期条件;$value=0 表示今天 |
| order_by | order_by($value, $value2 = null) | 如 order_by('id DESC') 或 order_by('id','DESC') |
| group_by | group_by($value) | 分组 |
| limit | limit($offset, $length = 0) | 仅传一个参数时表示条数;两个参数为 offset, length |
主键/唯一键冲突时替换整行,常用于种子数据。
XR_M()->table('member')->insert_batch([
['username' => 'a', 'status' => 1],
['username' => 'b', 'status' => 1],
]);
XR_M()->table('member')->update_batch([
['id' => 1, 'status' => 0],
['id' => 2, 'status' => 1],
], 'id');| 参数 | 说明 |
|---|---|
$id | 主键值;为 0 时可不按主键,改用链式 where / 第三个参数 |
$data | 要更新的关联数组,不能为空 |
$where | 可选原始 SQL 条件片段 |
// 按主键
XR_M()->table('member')->update(1, ['status' => 0]);
// 按条件
XR_M()->table('member')
->where('groupid', 2)
->update(0, ['status' => 0]);// 按主键删一条
XR_M()->table('member')->delete(10);
// 按条件
XR_M()->table('member')->where('status', 0)->delete();
// 按主键数组批量删
XR_M()->table('member')->delete_all([1, 2, 3]);| 方法 | 说明 |
|---|---|
get | 按主键取一行 |
getRow | 按链式条件取一行(limit 1) |
getField | 在 getRow 基础上取单个字段值 |
getAll | $num 限制条数;$key 非空时用该字段值作为结果数组的键 |
对当前 table() 指定的表执行驱动 truncate()。禁止业务侧手写 TRUNCATE TABLE。
XR_M()->table('cache')->clear_all();批量执行 SQL(插件 Install/Uninstall 等)。内部经 Table::_query → dr_format_create_sql → 驱动 formatCreateSql。成功返回空字符串,失败返回错误信息。
query_sql 会替换 {dbprefix};$more=1 返回多行,否则单行。
新业务的建表、改字段应优先用第 3、4 节 API;仅历史 SQL 脚本或无法结构化的语句再用 query_all。
多数写操作使用 dr_return_data:
// 成功 ['code' => 主键或1, 'msg' => '', 'data' => ...] // 失败 ['code' => 0, 'msg' => '错误说明', 'data' => ...]
insert / replace 成功时 code 为自增主键(或数据中的主键)。
create_table / drop_table / install_schema 成功时 code 为 1。
is_table_exists / is_field_exists 返回 1 或 0,不是 dr_return_data。
get / getAll / getRow 失败或无数据时多为空数组 []。
| 需求 | 推荐调用 |
|---|---|
| 表是否存在 | XR_M()->is_table_exists($table) |
| 字段是否存在 | XR_M()->is_field_exists($table, $name) |
| 创建表 | XR_M('table')->create_table(...) |
| 删除表 | XR_M('table')->drop_table(...) |
| 增加字段 | XR_M('table')->add_field(...) |
| 修改字段 | XR_M('table')->edit_field(...) |
| 删除字段 | XR_M('table')->drop_field(...) |
| 安装 Schema | XR_M('table')->install_schema($schema) |
| 插入 | XR_M()->table($t)->insert($data) |
| 更新 | XR_M()->table($t)->update($id, $data) |
| 删除 | XR_M()->table($t)->delete($id) |
| 查询 | XR_M()->table($t)->where(...)->getAll() |
| 清空表 | XR_M()->table($t)->clear_all() |
| 批量 SQL | XR_M()->query_all($sql) |