DeleteWhere

按条件标记删除数据, 返回删除行数。如模型定义时未开启 soft_deletes 则真删除数据记录。

deleteWhere 按条件标记删除数据, 返回删除行数

处理器

models.模型名称.DeleteWhere

参数表

参数类型说明示例
args[0]Object QueryParam查询条件{"wheres":[{"column":"name", "value":"张三"}]}

返回值

Integer 删除行数

示例一

数据模型

模型模型定义
user模型描述文件

处理器

models.user.DeleteWhere

参数表

[
{
"wheres": [{ "column": "status", "value": "enabled" }]
}
]

返回值

4

外部引用

在业务逻辑(Flow) 中调用:

{
"nodes": [
{
"name": "users",
"process": "models.user.DeleteWhere",
"args": [
{
"wheres": [{ "column": "status", "value": "enabled" }]
}
],
"outs": ["{{$out}}"]
}
]
}

在服务接口(API) 中调用:

{
"paths": [
{
"path": "/deletewhere",
"method": "POST",
"process": "models.user.DeleteWhere",
"in": [":params"],
"out": {
"status": 200,
"type": "application/json"
}
}
]
}
POST /api/user/deletewhere?where.status.eq=enabled

示例二

新建模型,新增models/category.mod.yao文件,写入以下内容:

{
"name": "书籍分类",
"table": {
"name": "category",
"comment": "书籍分类"
},
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "父级id",
"name": "parent_id",
"type": "integer",
"nullable": true
},
{
"label": "分类名称",
"name": "name",
"type": "string",
"length": 128,
"index": true
}
],
"relations": {
"book": {
"type": "hasMany",
"model": "book",
"key": "category_id",
"foreign": "id",
"query": {}
},
"parent": {
"type": "hasOne",
"model": "category",
"key": "id",
"foreign": "parent_id",
"query": {}
}
},
"option": {
"timestamps": true,
"soft_deletes": true
},
"values": [
{
"id": 1,
"parent_id": null,
"name": "文史类"
},
{
"id": 2,
"parent_id": 1,
"name": "历史"
},
{
"id": 3,
"parent_id": 1,
"name": "古诗"
},
{
"id": 4,
"parent_id": null,
"name": "理工类"
},
{
"id": 5,
"parent_id": 4,
"name": "数学"
},
{
"id": 6,
"parent_id": 4,
"name": "物理"
}
]
}

Deletewhere 批量删除,也是软删除

function Deletewhere() {
return Process("models.category.deletewhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Deletewhere

DeleteWhere

按条件标记删除数据, 返回删除行数。如模型定义时未开启 soft_deletes 则真删除数据记录。

deleteWhere 按条件标记删除数据, 返回删除行数

处理器

models.模型名称.DeleteWhere

参数表

参数类型说明示例
args[0]Object QueryParam查询条件{"wheres":[{"column":"name", "value":"张三"}]}

返回值

Integer 删除行数

示例一

数据模型

模型模型定义
user模型描述文件

处理器

models.user.DeleteWhere

参数表

[
{
"wheres": [{ "column": "status", "value": "enabled" }]
}
]

返回值

4

外部引用

在业务逻辑(Flow) 中调用:

{
"nodes": [
{
"name": "users",
"process": "models.user.DeleteWhere",
"args": [
{
"wheres": [{ "column": "status", "value": "enabled" }]
}
],
"outs": ["{{$out}}"]
}
]
}

在服务接口(API) 中调用:

{
"paths": [
{
"path": "/deletewhere",
"method": "POST",
"process": "models.user.DeleteWhere",
"in": [":params"],
"out": {
"status": 200,
"type": "application/json"
}
}
]
}
POST /api/user/deletewhere?where.status.eq=enabled

示例二

新建模型,新增models/category.mod.yao文件,写入以下内容:

{
"name": "书籍分类",
"table": {
"name": "category",
"comment": "书籍分类"
},
"columns": [
{
"label": "ID",
"name": "id",
"type": "ID",
"comment": "ID",
"primary": true
},
{
"label": "父级id",
"name": "parent_id",
"type": "integer",
"nullable": true
},
{
"label": "分类名称",
"name": "name",
"type": "string",
"length": 128,
"index": true
}
],
"relations": {
"book": {
"type": "hasMany",
"model": "book",
"key": "category_id",
"foreign": "id",
"query": {}
},
"parent": {
"type": "hasOne",
"model": "category",
"key": "id",
"foreign": "parent_id",
"query": {}
}
},
"option": {
"timestamps": true,
"soft_deletes": true
},
"values": [
{
"id": 1,
"parent_id": null,
"name": "文史类"
},
{
"id": 2,
"parent_id": 1,
"name": "历史"
},
{
"id": 3,
"parent_id": 1,
"name": "古诗"
},
{
"id": 4,
"parent_id": null,
"name": "理工类"
},
{
"id": 5,
"parent_id": 4,
"name": "数学"
},
{
"id": 6,
"parent_id": 4,
"name": "物理"
}
]
}

Deletewhere 批量删除,也是软删除

function Deletewhere() {
return Process("models.category.deletewhere", {
wheres: [{ column: "parent_id", value: 4 }],
});
}

运行 yao run scripts.test.Deletewhere