diff --git a/docs/fluent/relations.zh.md b/docs/fluent/relations.zh.md index 4dbbf408..f8916430 100644 --- a/docs/fluent/relations.zh.md +++ b/docs/fluent/relations.zh.md @@ -135,6 +135,8 @@ try await sun.$planets.create(earth, on: database) 让我们看一个 `Planet` 和 `Tag` 之间的多对多关系的例子。 ```swift +enum PlanetTagStatus: String, Codable { case accepted, pending } + // pivot 模型示例。 final class PlanetTag: Model { static let schema = "planet+tag" @@ -150,7 +152,7 @@ final class PlanetTag: Model { init() { } - init(id: UUID? = nil, planet: Planet, tag: Tag) throws { + init(id: UUID? = nil, planet: Planet, tag: Tag, comments: String?, status: PlanetTagStatus?) throws { self.id = id self.$planet.id = try planet.requireID() self.$tag.id = try tag.requireID() @@ -158,7 +160,7 @@ final class PlanetTag: Model { } ``` -Pivots 是包含两个 `@Parent` 关系的一般模型。一个用于每个要关联的模型。如果需要,可以将其他属性存储在 pivot 上。 +任何包含至少两个 `@Parent` 关系的模型,每个关系对应两个要关联的模型,可以作为一个枢纽(pivot)。该模型可以包含其他属性,例如其 ID,甚至可以包含其他 `@Parent` 关系。 向 pivot 模型添加 [unique](schema.zh.md#unique) 约束有助于防止冗余条目。请参阅[模式](schema.zh.md)了解更多信息。 @@ -197,13 +199,24 @@ final class Tag: Model { `@Siblings` 属性具有从关系中添加和删除模型的方法。 -使用 `attach` 方法向关系添加一个模型。这将自动创建并保存 `pivot` 模型。 +使用 `attach()` 方法将单个模型或模型数组添加到关系中。需要时,会自动创建并保存枢纽模型。可以指定回调闭包以填充每个创建的枢纽模型的其他属性。 ```swift let earth: Planet = ... let inhabited: Tag = ... // 添加模型到关系中。 try await earth.$tags.attach(inhabited, on: database) +// 在建立关联时填充枢纽模型的属性。 +try await earth.$tags.attach(inhabited, on: database) { pivot in + pivot.comments = "This is a life-bearing planet." + pivot.status = .accepted +} +// 将带有属性的多个模型添加到关系中。 +let volcanic: Tag = ..., oceanic: Tag = ... +try await earth.$tags.attach([volcanic, oceanic], on: database) { pivot in + pivot.comments = "This planet has a tag named \(pivot.$tag.name)." + pivot.status = .pending +} ``` 附加单个模型时,你可以使用 `method` 参数选择是否在保存之前检查关系。 diff --git a/docs/getting-started/hello-world.zh.md b/docs/getting-started/hello-world.zh.md index 8494052e..a07088bc 100644 --- a/docs/getting-started/hello-world.zh.md +++ b/docs/getting-started/hello-world.zh.md @@ -19,6 +19,9 @@ vapor new hello -n !!! tip "建议" 使用 `-n` 为所有的问题自动选择 no 来为您提供一个基本的模板。 +!!! tip "建议" + 你也可以不使用 Vapor Toolbox,直接从 GitHub 克隆[模板库](https://github.com/vapor/template-bare)来获取最新的模板。 + !!! tip "建议" Vapor 以及自带的模板默认使用 `async`/`await`。如果你的系统不能更新到 macOS 12 或者想继续使用 `EventLoopFuture`。运行命令时可以添加此标志 `--branch macos10-15`。 diff --git a/docs/leaf/overview.zh.md b/docs/leaf/overview.zh.md index 8954001d..1730eed4 100644 --- a/docs/leaf/overview.zh.md +++ b/docs/leaf/overview.zh.md @@ -17,7 +17,7 @@ Leaf 标签由四个元素组成: - 标记 `#`:这表示 leaf 解析器开始寻找的标记。 - 名称 `count`:标签的标识符。 - 参数列表 `(users)`:可以接受零个或多个参数。 -- 正文: 可以使用分号和结束标签为某些标签提供可选的正文。 +- 正文: 可以使用冒号和结束标签为某些标签提供可选的正文。 根据标签的实现,这四个元素可以有许多不同的用法。让我们来看几个例子,Leaf 内置标签是如何使用的: