[TOC]
## 数据配置
- 文件名称:data.php
> 用于配置数据库语句,跨数据源查询,使MySQL、SQLserver、Sqlite和Postgresql混合查询。
> 高级条件where语句、字段field关联配置,让语句一目了然。
> 注:当数据库表字段发生修改时需更新下缓存才能生效,因为字段信息保存到了缓存中
## 一个简单的例子
设置文件/html/www/demo/test/data/demo1/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'area',
'conn' => 'demo', //连接数据源,默认是domain.php中的demo的conn配置,可设置其他数据源
// 'limit' => 100 //默认最大数100
],
// 'title' => 'SEO: 默认是domain.php中的demo的title配置'
// 'keywords' => 'SEO: 关键词',
// 'description' => 'SEO: 描述',
];
```
设置文件/html/www/demo/test/data/demo1/set.php内容如下
```
<?php
return function ($data){
dump($data);
return $data;
};
```
打开网址[http://demo.tphp.com/test/data/demo1](http://demo.tphp.com/test/data/demo1)获得如下效果
- 效果是
- - 文件类型sql数据库
- - 在crm配置的数据库中查询area表
- - 默认查询最大数量为100

打开网址[http://demo.tphp.com/test/data/demo1.json](http://demo.tphp.com/test/data/demo1.json)获得如下效果
- 可以看到当后缀名为json时返回的是json数据格式,可以使用它来作为接口使用
#### 自动分页
默认每页20条记录
打开网址[http://demo.tphp.com/test/data/demo1?p=1](http://demo.tphp.com/test/data/demo1?p=1)获得如下效果
打开网址[http://demo.tphp.com/test/data/demo1?p=2](http://demo.tphp.com/test/data/demo1?p=2)获得如下效果
使用参数 psize 修改默认页数
打开网址[http://demo.tphp.com/test/data/demo1?p=2&psize=3](http://demo.tphp.com/test/data/demo1?p=2&psize=3)获得如下效果
## sqlfind例子
设置文件/html/www/demo/test/data/demo2/data.php内容如下
```
<?php
return [
'type' => 'sqlfind',
'config' => [
'table' => 'area',
]
];
```
设置文件/html/www/demo/test/data/demo2/set.php内容如下(和demo1中set.php相同)
打开网址[http://demo.tphp.com/test/data/demo2](http://demo.tphp.com/test/data/demo2)获得如下效果
sqlfind仅查询表中的第一条语句结果
## 字段配置
设置文件/html/www/demo/test/data/demo3/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'area',
'field' => [
'id',
'parent_id',
'title'
]
]
];
```
设置文件/html/www/demo/test/data/demo3/set.php内容如下(和demo1中set.php相同)
打开网址[http://demo.tphp.com/test/data/demo3](http://demo.tphp.com/test/data/demo3)获得如下效果
如果field不设置默认查询所有字段信息。
## 查询条件
设置文件/html/www/demo/test/data/demo4/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'area',
'field' => [
'id',
'parent_id',
'title'
],
'where' => [
[
['id', '=', 121],
['id', '=', 123],
'or',
['id', '=', [122, 133]]
],
'or',
['id', 'between', [100, 200]],
['id', '=', 111],
]
]
];
```
设置文件/html/www/demo/test/data/demo4/set.php内容如下(和demo1中set.php相同)
打开网址[http://demo.tphp.com/test/data/demo4](http://demo.tphp.com/test/data/demo4)获得如下效果
对应语句是:
```
select id, parent_id, title
from area
where
(id in (121, 123) or id in(122, 133))
or
(id between 100 and 200 and id in(111))
```
注:这里的 ‘=’ 相当于 in ,它会把相同的一组给 in 进来,避免了 id=121 and id=123 判断为假的情景
设置文件/html/www/demo/test/data/demo5/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'area',
'field' => [
'id',
'parent_id',
'title'
],
'where' => [
['id', 'between', [500, 1000]],
['title', 'like', '%东%'],
]
]
];
```
打开网址[http://demo.tphp.com/test/data/demo5](http://demo.tphp.com/test/data/demo5)获得如下效果
设置文件/html/www/demo/test/data/demo6/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'area',
'field' => [
'id',
'parent_id',
'title'
],
'where' => [
['id', 'between', [500, 1000]],
['title', 'like', '%东%'],
'or',
['id', '>', 2000],
['id', '<=', 2008],
['id', 'notbetween', [2003, 2006]]
]
]
];
```
打开网址[http://demo.tphp.com/test/data/demo6](http://demo.tphp.com/test/data/demo6)获得如下效果
设置文件/html/www/demo/test/data/demo7/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'item_info',
'field' => [
'id',
'item_name',
'item_subno',
'item_supn',
'int_date'
],
'where' => [
['item_subno', 'column', ['<', 'item_supn']],
['int_date', 'null'],
['item_name', 'like', '%糖%'],
['id', 'between', [10000, 10800]]
]
]
];
```
打开网址[http://demo.tphp.com/test/data/demo7](http://demo.tphp.com/test/data/demo7)获得如下效果
对应语句为
```
select id, item_name, item_subno, item_supn, int_date
from item_info
where
item_subno < item_supn
and int_date is null
and item_name like '%糖%'
and id between 10000 and 10800
```
## 条件命令语句通配符
- between
- notbetween
- null
- notnull
- column
- like
- =
- <>
- \>
- \>=
- <
- <=
## 字段关联查询
设置文件/html/www/demo/test/data/demo8/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'sale',
'conn' => 'demo',
'field' => [
'id',
[
[
'item_info',
'id',
'item_info_id', // sale 中的 item_info_id 字段
// 当为字符串时直接对应字段
'item_name'
]
],
'name',
[
[
// 其中demo为数据库配置连接,同样支持MySQL、SQLserver、Sqlite和Postgresql互用
['brand', 'demo'],
'id',
'brand_id', // sale 中的 brand_id 字段
[
// name as brand_name
'name' => 'brand_name',
'remark'
]
]
],
]
]
];
```
##### 步骤详解
1. 查询sale表中的id字段和name字段
2. sale表中的item_info_id和item_info中的id关联,取出item_info表中的item_name字段
3. sale表中的brand_id和brand中的id关联,取出name和remark字段
4. 因为sale中的name字段和brand中的name字段冲突,所以给brand中的name重新定义名字为brand_name
打开网址[http://demo.tphp.com/test/data/demo8](http://demo.tphp.com/test/data/demo8)获得如下效果
## 字段关联查询(多级查询)
设置文件/html/www/demo/test/data/demo9/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
'table' => 'sale',
'conn' => 'demo',
'field' => [
'id',
[
[
'item_info',
'id',
'item_info_id', // sale 中的 item_info_id 字段
'item_name'
],
[
'supcust',
'id',
'supcust_no', // item_info 中的 supcust_no 字段
[
'name' => 'supcust_name'
]
],
// 可以在这里继续加入层级关联,不限层级数
// [
// ...
// ]
],
'name'
]
]
];
```
##### 步骤详解
1. 查询sale表中的id字段和name字段
2. sale表中的item_info_id和item_info中的id关联,取出item_info表中的item_name字段
3. item_info表中的supcust_no和supcust中的id关联,取出supcust表中的name字段并重命名为supcust_name
打开网址[http://demo.tphp.com/test/data/demo9](http://demo.tphp.com/test/data/demo9)获得如下效果
## 查询优化说明
查询关联并非join或left join形式,而是通过分步查询,查询结果集去重后再进行下一个子节点搜索。
## 代码补充
如果以上配置达不到想要的查询结果,可以写自己的代码在里面。
#### 代码示例
设置文件/html/www/demo/test/data/ext/data.php内容如下
```
<?php
$table = 'item_info';
$conn = 'demo';
$supcust_name = trim($_GET['sname']);
$return = [
'type' => 'sql',
'config' => [
'table' => $table,
'field' => [
'id',
'item_name',
'item_no',
'int_date',
[
[
'supcust',
'id',
'supcust_no',
[
'name' => 'supcust_name'
]
]
]
]
]
];
if(empty($supcust_name)){
return $return;
}
if(true) { //if和else里面的效果是一样的
$supcust_list = $this->db('supcust')->where('name', 'like', "%{$supcust_name}%")->get();
}else{
$supcust_list = $this->db('supcust', $conn)->where('name', 'like', "%{$supcust_name}%")->get();
}
$supcust_ids = [];
foreach ($supcust_list as $supcust){
$supcust_ids[] = $supcust->id;
}
// 如果供应商ID未找到
if(empty($supcust_ids)){
return [
'default' => '没有数据'
];
}
$return['config']['where'] = [
['supcust_no', '=', $supcust_ids]
];
return $return;
```
打开网址[http://demo.tphp.com/test/data/ext](http://demo.tphp.com/test/data/ext)获得如下效果
打开网址[http://demo.tphp.com/test/data/ext?sname=百货](http://demo.tphp.com/test/data/ext?sname=百货)获得如下效果
打开网址[http://demo.tphp.com/test/data/ext?sname=百货99](http://demo.tphp.com/test/data/ext?sname=百货99)获得如下效果
#### 原生SQL语句
- 如果还是感觉以上配置达不到效果的时候,可以使用原生SQL语句
设置文件/html/www/demo/test/data/sql/data.php内容如下
```
<?php
return [
'type' => 'sql',
'config' => [
// 使用原生语句时,field、table和where设置将会失效,只有conn有效
// 一般不使用limit,方便统计分页,系统自动为其limit
'query' => 'select * from item_info'
]
];
```
打开网址[http://demo.tphp.com/test/data/sql](http://demo.tphp.com/test/data/sql)获得如下效果
打开网址[http://demo.tphp.com/test/data/sql?p=10&psize=5](http://demo.tphp.com/test/data/sql?p=10&psize=5)获得如下效果