Skip to content

数据模型关联

数据模型间支持 hasOnehasMany 方式关联,查询时通过 withs 参数,直接查询关联数据。

编写供应商 supplier 和用户 user 两个数据模型,一个用户对应一家供应商,一家供应商有多个用户。在查询用户时,同时返回所属供应商的信息,查询供应商时,同时返回该供应商的用户列表。

供应商模型 supplier:

字段标签
idID
name名称

数据示例:

ID名称
1象传智慧
2Yao App Engine

supplier.mod.json

json
{
  "name": "供应商",
  "table": { "name": "supplier", "comment": "供应商表" },
  "columns": [
    {
      "label": "ID",
      "name": "id",
      "type": "ID",
      "comment": "ID"
    },
    {
      "label": "名称",
      "name": "name",
      "type": "string",
      "index": true,
      "comment": "供应商名称"
    }
  ],
  "relations": {
    "users": {
      "name": "users",
      "type": "hasMany",
      "model": "user",
      "key": "supplier_id",
      "foreign": "id",
      "query": { "select": ["id", "name"] }
    }
  },
  "values": [
    { "id": 1, "name": "象传智慧" },
    { "id": 2, "name": "Yao App Engine" }
  ]
}

用户模型 user :

字段标签
IDid
supplier_id所属供应商 ID
name姓名

数据示例:

ID供应商名称
11张无忌
21李光富
32李木婷
42赵长青

user.mod.json

json
{
  "name": "用户",
  "table": { "name": "user", "comment": "用户表" },
  "columns": [
    { "label": "ID", "name": "id", "type": "ID", "comment": "ID" },
    {
      "label": "供应商",
      "name": "supplier_id",
      "type": "bigInteger",
      "index": true,
      "comment": "供应商ID"
    },
    {
      "label": "姓名",
      "name": "name",
      "type": "string",
      "index": true,
      "comment": "用户姓名"
    }
  ],
  "values": [
    { "id": 1, "supplier_id": 1, "name": "张无忌" },
    { "id": 2, "supplier_id": 1, "name": "李光富" },
    { "id": 3, "supplier_id": 2, "name": "李木婷" },
    { "id": 4, "supplier_id": 2, "name": "赵长青" }
  ]
}

创建数据表

bash
yao migrate

关联关系声明

关联关系通过 relations 中声明, 一个数据模型支持多个映射关系声明, 数据结构为{"relation_name":ObjectRelation, "relation_name":ObjectRelation ,...}

Object Relation 数据结构:

字段类型必填项说明
namestring关联名称,查询时通过这个名称引用
typeenum与当前数据模型的关系类型. hasOne 一对一, hasMany 一对多。
modelstring关联数据模型 名称
keystring关联数据模型 中用于关联的字段名称 (例如: user.supplier_id)
foreignstring当前数据模型 中用于关联的字段名称 (例如: supplier.id)
queryobject关联数据模型 的查询条件,可以在查询时重载。 例: { "select": ["id", "name"] }

hasOne

查询用户时,同时返回该用户所属供应商的信息。修改用户模型描述文件 user.mod.json,添加 hasOne 关联关系声明。

json
{
  "name": "用户",
  "table": { "name": "user", "comment": "用户表" },
  "columns": [
    { "label": "ID", "name": "id", "type": "ID", "comment": "ID" },
    {
      "label": "供应商",
      "name": "supplier_id",
      "type": "bigInteger",
      "index": true,
      "comment": "供应商ID"
    },
    {
      "label": "姓名",
      "name": "name",
      "type": "string",
      "index": true,
      "comment": "用户姓名"
    }
  ],
  "relations": {
    "supplier": {
      "name": "supplier",
      "type": "hasOne",
      "model": "supplier",
      "key": "id",
      "foreign": "supplier_id",
      "query": { "select": ["id", "name"] }
    }
  },
  "values": [
    { "id": 1, "supplier_id": 1, "name": "张无忌" },
    { "id": 2, "supplier_id": 1, "name": "李光富" },
    { "id": 3, "supplier_id": 2, "name": "李木婷" },
    { "id": 4, "supplier_id": 2, "name": "赵长青" }
  ]
}

数据查询

查询用户同时,查询所有供应商信息:

bash
yao run models.user.Find 1 '::{"withs":{ "supplier": {} }}'
yao run models.user.Get '::{"withs":{ "supplier": {} }}'

指定关联模型的选取字段:

bash
yao run models.user.Find 1 '::{"withs":{ "supplier": {"query":{ "select":["name"] }} }}'

按关联模型的字段查询条件:

bash
yao run models.user.Get '::{"withs":{ "supplier": {} }, "wheres":[{"rel":"supplier", "column":"name", "value":"Yao App Engine" }]}'

在 JS 中使用任意关联关系:

javascript
function test() {
  var user = Process('models.user.get', {
    withs: {
      supplier: {
        query: {
          select: ['name', 'id']
        }
      }
    },
    wheres: [{ column: 'supplier_id', value: 1, op: '=' }],
    orders: [{ column: 'id', option: 'desc' }],
    limit: 1
  });
}

hasMany

查询供应商时,同时返回该供应商的用户信息。修改用户模型描述文件 supplier.mod.json,添加 hasMany 关联关系声明。

json
{
  "name": "供应商",
  "table": { "name": "supplier", "comment": "供应商表" },
  "columns": [
    { "label": "ID", "name": "id", "type": "ID", "comment": "ID" },
    {
      "label": "名称",
      "name": "name",
      "type": "string",
      "index": true,
      "comment": "供应商名称"
    }
  ],
  "relations": {
    "users": {
      "name": "users",
      "type": "hasMany",
      "model": "user",
      "key": "supplier_id",
      "foreign": "id",
      "query": { "select": ["id", "name"] }
    }
  },
  "values": [
    { "id": 1, "name": "象传智慧" },
    { "id": 2, "name": "Yao App Engine" }
  ]
}

数据查询

查询供应商时,同时查询所属用户信息:

bash
yao run models.supplier.Find 1 '::{"withs":{ "users": {} }}'
yao run models.supplier.Get '::{"withs":{ "users": {} }}'

指定关联模型的选取字段:

bash
yao run models.supplier.Find 1 '::{"withs":{ "users": { "query":{"select":["name"] }} }}'

指定关联用户筛选条件:

bash
yao run models.supplier.Find 1 '::{"withs":{ "users": { "wheres":[{"column":"name", "value":"张无忌"}] } }}'

嵌套查询

关联关系支持嵌套查询,通过 withs 查询参数指定。

bash
yao run models.supplier.Find 1 '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'
bash
yao run models.supplier.Get '::{"withs":{ "users": { "withs": {"supplier":{} } } }}'