1. dbschema数据表定义
1.1. 简介
通过dbschema配置文件, 定义数据库
- 描述表结构, 生成对应的实体表
- 定义desktop finder的属性
dbschema文件存放在各自app下的dbschema
目录, 例如: app/{$appId}/dbschema/{$name}.php
, 表名: {$appId}_($name}
以下是base_kvstore
表的定义(由以上规则可知dbschema文件所在位置, app/base/dbschema/kvsore.php
)
return array (
'columns' =>
array (
'id' => array(
'type' => 'number',
'extra' => 'auto_increment',
'comment' => app::get('base')->_('序号'),
),
'prefix' => array(
'type' => 'string',
'required' => true,
'comment' => app::get('base')->_('kvstore类型'),
),
'key' => array(
'type' => 'string',
'required' => true,
'comment' => app::get('base')->_('kvstore存储的键值'),
),
'value' => array(
'type'=>'serialize',
'comment' => app::get('base')->_('kvstore存储值'),
),
'dateline' => array(
'type'=>'time',
'comment' => app::get('base')->_('存储修改时间'),
),
'ttl' => array(
'type'=>'time',
'default' => 0,
'comment' => app::get('base')->_('过期时间,0代表不过期'),
),
),
'primary' => 'id',
'index' => array (
'ind_prefix' => ['columns' => ['prefix']],
'ind_key' => ['columns' => ['key']],
),
'comment' => app::get('base')->_('kvstore存储表'),
);
1.2. DBAL类型
下表是dbal类型与mysqll类型的对应表, 其中required
和comment
属性是所有dbal类型天生具备的.
doctrine类型 | 默认mysql类型 | length | precision | scale | autoincrement | unsigned | fixed |
---|---|---|---|---|---|---|---|
array | text | √ | × | × | × | × | × |
array_simple | text | × | × | × | × | × | × |
json_array | text | × | × | × | × | × | × |
bigint | bigint | × | × | × | √ | √ | × |
boolean | tinyint(1) | × | × | × | × | × | × |
datetime | datetime | × | × | × | × | × | × |
datetimez | datetime | × | × | × | × | × | × |
date | date | × | × | × | × | × | × |
time | time | × | × | × | × | × | × |
decimal | numeric(10,0) | × | √ | √ | × | × | × |
integer | int | × | × | × | √ | √ | × |
object | text | × | × | × | × | × | × |
smallint | smallint | × | × | × | √ | √ | × |
string | varchar(255) | √ | × | × | × | × | √ |
text | longtext | × | × | × | × | × | × |
binary | varbinary(255) | √ | × | × | × | × | √ |
blob | longblob | × | × | × | × | × | × |
float | double precision | × | × | × | × | × | × |
guid | char(36) | × | × | × | × | × | × |
text | longtext | × | × | × | × | × | × |
1.3. 内置预定义类型
buildin type | DBAL type | options |
---|---|---|
bool | boolean | |
money | decimal | ['precision' => 20, 'scale' = 3] |
string | ||
time | integer | ['unsigned' => 'true'] |
region | string | |
password | string | ['length' => 32] |
number | integer | ['unsigned' => true] |
float | float | |
gender | string | ['length' => 6] |
ipaddr | string | ['length' => 20] |
serialize | text | |
last_modify | integer | ['unsigned' => true] |
1.4. 表结构字段定义
每个字段需要指定type
(类型), 类型支持四种方式DBAL类型/内置预定义类型/表关联类型/enum类型
DBAL类型/内置预定义类型可参照上文的两张对应表.
DBAL类型需要根据具体类型, 定义对应的选项. 例如, decimal
类型对应着precision和scale选项. 如果不填,默认分别为10和0.
以下是DBAL类型:decimal的例子
return array(
'weight => array(
'type' => 'decimal',
'precision' => 20,
'scale => 2,
'required => true,
'default' => 0,
),
);
以下是DBAL类型:integer的例子
return array(
'id => array(
'type' => 'integer',
),
);
以下是DBAL类型:bigint的例子
return array(
'id => array(
'type' => 'bigint',
'autoincrement => true,
),
以下是DBAL类型:bigint的例子
return array(
'id => array(
'type' => 'bigint',
'autoincrement => true,
),
);
内置预定义类型, 对应着DBAL类型. 但与DBAL类型的设置不同, 不需要填写额外的选项, 因为内置预定义类型已经预设了选项. 例如: money
类型对应着DBAL类型的decimal
, precision
为20, scale
为3, 如果对应着mysql的类型为numeric(10,0)
以下是内置预定义类型:money
return array(
'price' => array(
'type' => 'money'
),
);
表关联类型, 按照关联表对应字段的类型建立本字段. 例如, 商品表(sysitem_item)
存在类目ID(cat_id)
, 那么在设置这个字段类型时最好的方式是指定跟syscategory
表的cat_id
一样就行了
以下是表关联类型的例子, sysitem_item
的cat_id
关联syscategory_cat的
cat_id`
return array(
'cat_id => array(
'type' => 'table:cat@syscategory'
),
);
注意:
表关联类型
, 只是作为生成字段时的参照, 没有其他用途
- enum类型, type里直接写数组就可以了, 其中
key
为enum
里的值,value
为comment
以下是enum类型的例子
return array(
'status' => array(
'type' => array(
'WAIT_BUYER_PAY' => '等待买家付款',
'WAIT_SELLER_SEND_GOODS' => '等待卖家发货,即:买家已付款',
'WAIT_BUYER_CONFIRM_GOODS' => '等待买家确认收货,即:卖家已发货',
'TRADE_BUYER_SIGNED' => '买家已签收,货到付款专用',
'TRADE_FINISHED' => '交易成功',
'TRADE_CLOSED_AFTER_PAY' => '付款以后,用户退款成功,交易自动关闭',
'TRADE_CLOSED_BEFORE_PAY' => '付款以前,卖家或买家主动关闭交易',
),
'default' => 'WAIT_BUYER_PAY',
'required' => true,
'comment' => app::get('systrade')->_('子订单状态'),
),
);
1.5. 表结构索引定义
- 主键定义
当只有一个主键的时候
return array(
'columns' => array(
'cart_id' => array(
'type' => 'number',
//'pkey' => true,
'autoincrement' => true,
'required' => true,
),
),
'prmary' => 'cart_id'
);
当有多主键的时候
return array (
'columns' =>
array (
'id' =>
array (
'type' => 'number',
'required' => true,
),
'index_name' =>
array (
'type' => 'string',
'length' => 50,
'required' => true,
),
//...
),
'primary' => ['id', 'index_name'],
'index' => array(
'ind_last_modify' => ['columns' => ['last_modify']],
),
'version' => '$Rev: 40918 $',
);
注意: 建议使用一个自增主键. 如果当有多主键的时候, 可以设置一个自增主键, 同时将多键作为
unique key
. 如果使用多主键在使用finder时会出现诡异问题.
- 索引定义
return array (
'columns' => array (
//...
'last_modify' =>
array (
'type' => 'last_modify',
),
//...
),
'index' => array( //索引名称
'ind_last_modify' => [
'columns' => ['last_modify'], // 需要建立索引的字段名
'prefix' => 'unique' // 目前只支持unqiue, 或者不填写
],
),
);
1.6. finder定义
用dbschema来定义desktop列表
label定义
定义在desktop finder
中列的名称
return array (
'columns' => array (
'app_id' => array (
'label' => app::get('base')->_('程序目录'),
),
),
);
?>
width定义
定义在desktop finder
中列的初始宽度
return array (
'columns' => array (
'app_id' => array (
'label' => app::get('base')->_('程序目录'),
'width => 150,
),
),
);
in_list定义
定义在desktop finder
配置列表项中是否可以勾选显示, 默认值为false.
return array (
'columns' => array (
'app_id' => array (
'label' => app::get('base')->_('程序目录'),
'width => 150,
'in_list' => true,
),
),
);
default_in_list定义
定义在desktop finder
列表中初始安装的情况下, 对应列是否默认显示在列表中, 默认值为false.
return array (
'columns' => array (
'app_id' => array (
'label' => app::get('base')->_('程序目录'),
'width => 150,
'default_in_list' => true,
),
),
);
filterdefault定义
默认在desktop高级筛选(搜素), 中是否默认显示, 默认为false. 如果有相关搜索项配置(filtertype), 按配置显示
return array (
'columns' => array (
'app_id' => array (
'label' => app::get('base')->_('程序目录'),
'width' => 150,
'in_list' => true,
'filterdefault' => true,
),
),
);
filtertype, filtercustom定义
return array (
'columns' => array (
'name' => array (
'type' => 'string',
'required' => true,
'label' => app::get('b2c')->_('商品名称'),
'is_title' => true,
'filtertype' => 'custom', // normal按type类型生成过滤 , custom按dbschema中设定的filtercustom 设置过滤
'filterdefault' => true,
'filtercustom' =>
array (
'has' => app::get('b2c')->_('包含'),
'tequal' => app::get('b2c')->_('等于'),
'head' => app::get('b2c')->_('开头等于'),
'foot' => app::get('b2c')->_('结尾等于'),
),
'in_list' => true,
'default_in_list' => true,
),
),
);
注意:
filtertype
属性, normal按type类型生成过滤 , custom按dbschema中设定的filtercustom 设置过滤
searchtype
finder列表页中简单搜索的处理方式,如果dbschema中存在searchtype则会在desktop列表上显示相关的简单搜索
默认值为nequal
return array (
'columns' => array (
'title' => array(
'type' => 'string',
'length' => 90,
'required' => true,
'default' => '',
'label' => app::get('sysitem')->_('商品标题'),
'comment' => app::get('sysitem')->_('商品标题'),
'is_title' => true,
'searchtype' => 'has',
'filtertype' => 'custom',
'filterdefault' => true,
'order' => 12,
),
),
);
目前支持类型
类型 | 描述 |
---|---|
than | 大于 |
小于 | < |
nequal | 等于 |
tequal | 等于 |
sthan | 小于等于 |
bthan | 大于等于 |
has | 包含 |
head | 头部 |
foot | 尾部 |
nohas | 不包含 |
between | 介于之间 |
in | 在之中 |
注意: 请尽量不要用foot类型, 因为会用到左like, 会影响性能.
is_title定义
例如, 商品表有cat_id(类目表ID)字段, 那么在finder上cat_id字段会显示类目表中设置为is_title的字段(cat_name)的值.
注意: 同时一张表只能有一个字段能设置为is_title属性.
商品表
return array (
'columns' => array (
'cat_id' => array (
'label' => app::get('base')->_('商品类目ID),
'width => 150,
'in_list' => true,
'filterdefault' => true,
),
),
);
类目表
return array (
'columns' => array (
'cat_name => array (
'label' => app::get('base')->_('分类名称'),
'is_title' => true,
'width => 150,
'in_list' => true,
'filterdefault' => true,
),
),
);
order定义
order属性定义了在desktop finder
上列的默认排列顺序.
return array (
'columns' => array (
'cat_name => array (
'label' => app::get('base')->_('程序目录'),
'order' =>10,
),
),
);
1.7. 其他定义
unbackup
当设置为unbackup
时, 备份数据时将不会备份此表数据
注意: 备份数据功能只供备份初始化数据, 不再提供生成环境备份用途.
return array (
'columns' => array (
//...
),
'unbackup' => true,
);
comment
表名注释
return array (
'columns' => array (
//...
),
'comment => '商品表',
);