查询
登录MassCMS系统后,侧边栏选择内容模型,点击创建新的集合类型,创建一个数据库名 moment 的模型。

并添加 content , mDate , pictureList 三个字段。数据库即会动态生成一张名为项目AppId+_+数据库名的表(如P88666_moment)。

项目AppId可在设置-项目设置-项目信息中查询。

1.查询单条数
接口地址:https://saas.masscms.com/cms-api/cms/api/get
请求参数:
{
  "P88666_moment":{
    "id":1692611986409
  }
}响应:
{
    "P88666_moment": {
        "id": 1692611986409,
        "create_user": "jcr",
        "create_time": "2023-08-21 17:59:46",
        "update_user": "jcr",
        "update_time": "2023-08-21 17:59:46",
        "mass_content_control_status": "N",
        "content": "今天天气不错,到处都是提拉米苏雪",
        "mDate": "2023-08-20 08:00:00",
        "pictureList": ""
    },
    "ok": true,
    "code": 200,
    "msg": "success",
}可以通过内容模型中定义的任何唯一的字段提取单条数据,当使用的字段不是唯一的时,只会返回第一条数据。
2.查询多条数据
接口地址:https://saas.masscms.com/cms-api/cms/api/get
请求参数:
{
  "[]": {
    "P88666_moment": {
        "@column": "id,content,mDate,pictureList",
        "@order": "mDate-,id,content+"
    },
    "query":2
  },
  "total@": "/[]/total"
}响应:
{
    "[]": [
        {
            "P88666_moment": {
                "id": 1692757999911,
                "content": "今天天气不错",
                "mDate": "2023-08-23 00:00:00",
                "pictureList": ""
            }
        },
        {
            "P88666_moment": {
                "id": 1692758038614,
                "content": "今天又下雨了!",
                "mDate": "2023-08-15 00:00:00",
                "pictureList": ""
            }
        }
    ],
    "total": 2,
    "ok": true,
    "code": 200,
    "msg": "success"
}其中响应结果中"total":2是因为在发送数据中增加了query:2和"total":"/[]/total"响应结果返回当前表的总数。
如果query:1时,结果将只会返回当前表的总数。
{ "total": 2, "code": 200, "msg": "success" }3.关联查询
在讲解关联查询的时候,我们需要先了解下表之间的关系
现在有两张表 USER 和 MOMENT,两张表的关系是下面这样

MOMENT 表示评论,每一条评论会有一个发表评论的用户 USER,所以 MOMENT 表里会有一个 USER 表的外键关联
对于这样的数据关系,我们在查询评论时,很多时候我们会连带着用户一起查处来,这样又如何操作呢?
{
  "[]": {
    "Moment": {
      "@column": "id,date,userId",
      "id": 12
    },
    "User": {
      "id@": "/Moment/userId",
      "@column": "id,name"
    }
  }
}这个请求稍微复杂点,首先我们用[]对象表示我们是想查询出一个列表,这个列表包含两个部分Moment和User。
其中Moment是我们想要查询的主要内容,它的写法也和一般查询数据时无异。
User是与Moment相关联的数据,所以查询的时候我们需要用id@来表示他们之间的关联关系
/Moment/userId中,最开始的/相当于是指明了[]的位置,/Moment表示[]对象下的Moemnt对象,/Moment/userId表示Moemnt的userId字段是与User的id关联的。
响应的数据:
{
  "[]": [
    {
      "Moment": {
        "date": "2017-02-08 16:06:11.0",
        "id": 12,
        "userId": 70793
      },
      "User": {
        "id": 70793,
        "name": "Strong"
      }
    }
  ],
  "code": 200,
  "msg": "success"
}在MASSCMS系统有两种方式可以建立关联关系,一是在模型中添加组件,二是在模型中添加关联。

3.1 集合/单一模型添加组件
首先创建一个数据库名为 user 的自定义组件,系统会自动生成一张名为项目AppId+_c_+组件数据库名的表,如项目P54270_c_user

然后在Moment模型中添加该组件。系统会自动生成名为项目AppId+_ + 模型数据库名 + _c_+组件数据库名的关联表,如项目P54270_moment_c_user

3.1.1 多表一对一关联查询
当查询单条数据时,发送数据:
{
    "P54270_moment": {
        "id": "1692774640609"
    },
    "P54270_moment_c_user": {
        "entity_id@": "P54270_moment/id",
    },
    "P54270_c_user": {
        "id@": "P54270_moment_c_user/component_id"
    }
}3.1.2 多表一对多关联查询
当关联的 user 是集合数据时,发送数据:
{
    "P54270_moment": {
        "id": "1692774640609"
    },
    "user[]": {
        "P54270_moment_c_user": {
            "entity_id@": "P54270_moment/id"
        },
        "P54270_c_user": {
            "id@": "user[]/P54270_moment_c_user/component_id"
        }
    }
}请注意,这里的user[]其中[]表示请求的对象是一个数组,user是自定义的标识,当P54270_moment有多个关联关系时可以用以区分。比如在Moment模型中再添加一个comment组件。请求数据如下:
{
    "P54270_moment": {
        "id": "1692774640609"
    },
    "user[]": {
        "P54270_moment_c_user": {
            "entity_id@": "P54270_moment/id"
        },
        "P54270_c_user": {
            "id@": "user[]/P54270_moment_c_user/component_id"
        }
    },
    "comment[]": {
        "P54270_moment_c_comment": {
            "entity_id@": "P54270_moment/id"
        },
        "P54270_c_comment": {
            "id@": "comment[]/P54270_moment_c_comment/component_id"
        }
    }
}此处关联的是不同的组件,但当Moment模型需要关联同一个组件时,又当如何处理呢?比如在Moment添加一个单一类型的组件user表示当前用户,再添加一个集合类型的组件user表示当前点赞的用户。

这种情况可以使用上述请求,获得Moment所有关联的user数据,然后根据返回结果中关联关系表P54270_moment_c_user的filed字段对结果进行处理。还可以使用另外一种方法,在请求数据时即通过filed字段对数据进行过滤。如下:
{
    "P54270_moment": {
        "id": "1692778412077"
    },
    "P54270_moment_c_user": {
        "entity_id@": "P54270_moment/id",
        "filed": "currentUser"
    },
    "P54270_c_user": {
        "id@": "P54270_moment_c_user/component_id"
    },
    "userlike[]": {
        "P54270_moment_c_user": {
            "entity_id@": "P54270_moment/id",
            "filed": "likeUser"
        },
        "P54270_c_user": {
            "id@": "userlike[]/P54270_moment_c_user/component_id"
        }
    }
}当然当关联的user组件都为集合类型时,在[]前设置不同的标识即可,如:
{
    "P54270_moment": {
        "id": "1692778412077",
        "@column": "id"
    },
    "user[]": {
        "P54270_moment_c_user": {
            "entity_id@": "P54270_moment/id",
            "filed": "currentUser",
            "@column": "filed,component_id"
        },
        "P54270_c_user": {
            "id@": "user[]/P54270_moment_c_user/component_id"
        }
    },
    "userlike[]": {
        "P54270_moment_c_user": {
            "entity_id@": "P54270_moment/id",
            "filed": "likeUser",
            "@column": "filed,component_id"
        },
        "P54270_c_user": {
            "id@": "userlike[]/P54270_moment_c_user/component_id"
        }
    }
}响应结果:
{
    "P54270_moment": {
        "id": 1692778412077
    },
    "user[]": [
        {
            "P54270_moment_c_user": {
                "filed": "currentUser",
                "component_id": 1692785003032
            },
            "P54270_c_user": {
                "id": 1692785003032,
                "name": "卡卡西",
                "sex": "1",
                "age": 18
            }
        }
    ],
    "userlike[]": [
        {
            "P54270_moment_c_user": {
                "filed": "likeUser",
                "component_id": 1692785003037
            },
            "P54270_c_user": {
                "id": 1692785003037,
                "name": "澹台明月",
                "sex": "2",
                "age": 20
            }
        },
        {
            "P54270_moment_c_user": {
                "filed": "likeUser",
                "component_id": 1692785003043
            },
            "P54270_c_user": {
                "id": 1692785003043,
                "name": "南宫仆射",
                "sex": "2",
                "age": 22
            }
        }
    ],
    "ok": true,
    "code": 200,
    "msg": "success"
}特别需要注意的是
"id@": "user[]/P54270_moment_c_user/component_id"
"id@": "P54270_moment_c_user/component_id"
"id@": "userlike[]/P54270_moment_c_user/component_id"
等表示关联关系条件的语句要根据请求数据的结构和关系正确书写,从最外层开始依次递进。
3.1.3 多表数组内一对一关联查询
查询Moment及其关联的user 时,发送数据:
{
    "[]": {
        "P54270_moment": {},
        "P54270_moment_c_user": {
            "entity_id@": "/P54270_moment/id",
            "@column": "component_id,filed"
        },
        "P54270_c_user": {
            "id@": "/P54270_moment_c_user/component_id"
        }
    }
}响应:
{
    "[]": [
        {
            "P54270_moment": {
                "id": 1692774640609,
                "create_user": "jcr",
                "create_time": "2023-08-23 15:10:40",
                "update_user": "jcr",
                "update_time": "2023-08-23 15:10:40",
                "mass_content_control_status": "N",
                "content": "今天天气还是不错的",
                "mDate": "2023-08-10"
            },
            "P54270_moment_c_user": {
                "component_id": 1692774640635,
                "filed": "user"
            },
            "P54270_c_user": {
                "id": 1692774640635,
                "name": "user",
                "sex": "1",
                "age": 18
            }
        },
        {
            "P54270_moment": {
                "id": 1692778412077,
                "create_user": "jcr",
                "create_time": "2023-08-23 16:13:31",
                "update_user": "jcr",
                "update_time": "2023-08-23 16:13:31",
                "mass_content_control_status": "N",
                "content": "今天又下起了小雪",
                "mDate": "2023-08-15"
            },
            "P54270_moment_c_user": {
                "component_id": 1692778412095,
                "filed": "user"
            },
            "P54270_c_user": {
                "id": 1692778412095,
                "name": "卡卡西",
                "sex": "1",
                "age": 20
            }
        }
    ],
    "ok": true,
    "code": 200,
    "msg": "success"
}3.2 集合/单一模型关联集合模型
在MASSCMS系统中集合类型和单一类型的模型可以选择关联集合类型。如下所示创建了一个News的集合类型,然后在Moment模型中关联该News模型,可以选择一对一关联,也可选择一对多关联。

建立关联关系后系统会自动创建名为项目AppId+_ + 模型数据库名 + _+被关联模型数据库名+_link的关联表,如P54270_moment_news_link。
3.2.1 多表数组内一对多关联查询
查询示例如下:
{
    "[]": {
        "P54270_moment": {
            "@order": "create_time-"
        },
        "P54270_moment_news_link_[]": {
            "P54270_moment_news_link": {
                "entity_id@": "[]/P54270_moment/id"
            },
            "P54270_news": {
                "id@": "[]/P54270_moment_news_link_[]/P54270_moment_news_link/entity_link_id"
            }
        }
    }
}示例为一对多关联的列表查询,一对一关联,单个数据查询可参考上述关联查询相关示例自行修改即可。
需要注意的是P54270_news和P54270_moment本身是没有直接关联的,此处查询必须借助P54270_moment_news_link关联表。在MASSCMS系统中添加关联时,合理设置展示字段,将关联表中的show_value字段与P54270_news的字段建立联系,以便于在实际业务过程中正确查询。
4.分组查询
在了解分组查询之前,我们需要先了解下 APIJSON 所支持的函数
| 函数名 | 说明 | 
| count | 统计分组下,某字段的个数 | 
| sum | 统计分组下,某字段的和 | 
| max | 统计分组下,某字段的最大值 | 
| min | 统计分组下,某字段的最小值 | 
| avg | 统计分组下,某字段的平均值 | 
比如,如果只是单纯的查出最大值,这样请求就可以了
{
  "[]": {
    "Moment": {
      "@column": "max(id):maxid"
    }
  }
}响应:
{
  "[]": [
    {
      "Moment": {
        "maxid": 1541912160047
      }
    }
  ],
  "code": 200,
  "msg": "success"
}这里maxid是我们取的别名
如果是有分组条件的,那我们需要使用@group
比如,像下面 SALE 表,这张表表示,2018 年 1 月 1 日某公司门下的 3 个店铺(STORE_ID)的营业额(AMT)数据
| ID | STORE_ID | AMT | 
| 1 | 1 | 100 | 
| 2 | 1 | 80 | 
| 3 | 2 | 30 | 
| 4 | 2 | 100 | 
| 5 | 3 | 210 | 
如果,我们想要计算出这天每个店铺一共卖了多少,我们通过 APIJSON 可以这样查询
{
  "[]": {
      "Sale":{
        "@column":"store_id,sum(amt):totAmt",
        "@group":"store_id"
      }
  }
} 
  
  
  
  MassCMS
          MassCMS
        






 
  
  
  
 






 
  
 