1,表单模块表xxx_fields_data
2,自定义字段表xxx_fields
id |
field_data_id |
is_required |
field |
name |
type |
两张表的关系xxx_fields_data.id=xxx_fields.field_data_id
使用场景,由于不同模块的表前缀不一样,所以在A模块使用XXX模块下的这两张表,表名总会补充成 A_xxx_tableName(正确的应该只是xxx_tableName)
想得到的sql结果:
SELECT `field`,`name`,`is_required`,`type` FROM `xxx_fields_data` `fields_data` LEFT JOIN `xxx_fields` `fields` ON `fields`.`field_data_id`=`fields_data`.`id` WHERE `module` = 'customer' AND ( `is_required` = '1' OR `type` IN ('email','integer','telephone') )
连表查询代码如下:
$list = Db::table('xxx_fields_data')
->alias('fields_data')
->join(['xxx_fields'=>'fields'],'fields.field_data_id=fields_data.id','left')
->where('module','=',$module)
->where(function ($query) {
$query->where('is_required', 1)
->whereOr('type', 'in', $this->check_type);
})
->field('field,name,is_required,type')
->select();
重点就在标红区域,给表设定表的别名
|