关系
定义关系
关系无需额外声明。字段的类型如果是另一个模型,就会将两者连接起来,KScript.Data 会根据两侧的字段推断出连接的类型:
<import package="KScript.Data" />
<model name="User">
<field name="name" type="string" />
<field name="posts" type="Post[](by: author)" />
<field name="profile?" type="Profile" />
</model>
<model name="Profile">
<field name="user" type="User" />
<field name="bio" type="string" />
</model>
<model name="Post">
<field name="title" type="string" />
<field name="published" type="bool" default="false" />
<field name="author" type="User" />
<field name="tags" type="Tag[]" />
</model>
<model name="Tag">
<field name="name" type="string" />
<field name="posts" type="Post[]" />
</model>import KScript.Data
model User {
name: string
posts: Post[](by: author)
profile?: Profile
}
model Profile {
user: User
bio: string
}
model Post {
title: string
published: bool = false
author: User
tags: Tag[]
}
model Tag {
name: string
posts: Post[]
}下面说明每种字段的含义。
属于(belongs to)。Post 上的 author: User 会存储一个 author_id 列,即指向 users 的外键。该模型也会得到一个 author_id 字段。
拥有一个(has one)。User 上的 profile?: Profile(其中 Profile 有 user: User)会保存为 user_id,位于 profiles 上。拥有一个的一方是可选一方。
拥有多个(has many)。User 上的 posts: Post[],其中 Post 有一个类型为 User 的字段,会使用该字段在 posts 上的列。当 Post 有多个这样的字段(比如 author 和 editor)时,请用 by: 指明你要的那一个,例如 posts: Post[](by: author)。
多对多(many to many)。Post 上的 tags: Tag[],其中 Tag 拥有的是 Post 的列表而非单个 Post,会保存在名为 post_tag 的中间表中,包含 post_id 和 tag_id。表名可用 pivot: 自行指定。
通过中间关系拥有多个(has many through)。comments: Comment[](through: posts) 通过另一个关系读取该用户帖子的评论。它本身不存储任何内容。
关系也可以只保留另一模型某个 作用域 所保留的记录:drafts: Post[](by: author, scope: drafts)。
在属于(belongs-to)字段上,on_delete: 指定所指向的记录被删除时会发生什么。cascade 会同时删除本记录,是必填字段的默认行为。set_null 会清空该字段,是可选字段的默认行为。restrict 会拒绝删除,并抛出 ForeignKeyViolation 错误。
两个相互引用的模型可以各自位于单独的文件中,而无需互相导入。当字段指定的模型没有被导入时,会在项目的模型文件夹中查找(即 kscript.json 中的 paths.models)。这是唯一无需导入即可找到模型的情况,否则两个文件会相互导入,而循环导入是不被允许的。使用该模型的脚本和控制器仍需导入其文件。参见每个文件一个类型。
读取关系
要读取关系,像读取字段一样读取即可。属于和拥有一个的关系会得到一条记录(或 null),其余的则得到一个列表:
<let name="ada" value="User->create(name: 'Ada')" />
<let name="post" value="$ada->posts()->create(title: 'Notes on the engine')" />
<echo value="$post.author.name" />
<echo value="$ada.posts->map(.title)" />$ada = User->create(name: 'Ada')
$post = $ada->posts()->create(title: 'Notes on the engine')
echo $post.author.name
echo $ada.posts->map(.title)当你在来自同一列表的记录上读取关系时,会一次性为整个列表加载。像下面这样的循环只会发出一次查询来获取所有作者,而不是为每篇帖子各查询一次:
<for each="post" in="Post->latest()->get()">
<echo>{$post.title} by {$post.author.name}</echo>
</for>for post in Post->latest()->get() {
echo "{$post.title} by {$post.author.name}"
}预先加载关系
->with() 会在加载记录的同时加载关系。带点号的名称会加载关系自身的关系:
<let name="users" value="User->with(profile, posts.tags)->get()" />$users = User->with(profile, posts.tags)->get()如果你只想要部分相关记录,可以传入一个用于修改关系查询的函数:
<let name="users" value="User->with(posts: fn(q) => $q->where(published: true))->get()" />$users = User->with(posts: fn(q) => $q->where(published: true))->get()->load() 对你已有的记录执行同样的操作:
<do>$post->load(author, tags)</do>$post->load(author, tags)未加载的关系会被排除在 JSON 之外,因此路由的响应只包含你加载过的关系。
若要统计相关记录的数量而不读取它们,请使用 ->with_count()。每条记录都会获得一个 posts_count 字段,由数据库计算得出:
<let name="authors" value="User->with_count(posts)->get()" />$authors = User->with_count(posts)->get()查询关系
将关系作为方法调用(如 $ada->posts())会得到相关记录的查询。你可以对其进行过滤、计数或追加条件,且结果都限定在该用户的帖子范围内:
<import package="KScript.Data" />
<echo value="$ada->posts()->where(published: false)->count()" />
<do>$ada->posts()->create(title: 'A second post')</do>import KScript.Data
echo $ada->posts()->where(published: false)->count()
$ada->posts()->create(title: 'A second post')关系上的 ->create() 会为你设置链接:新帖子的作者为 Ada。
多对多关系通过其中间表进行链接。->attach() 会链接记录(或键,或两者之一的列表),并保留已存在的链接。->detach() 会解除你给出的记录的链接,或解除全部链接。->sync() 会精确链接你给出的记录并解除其余记录的链接,->toggle() 则会对每条记录进行切换:
<import package="KScript.Data" />
<let name="maths" value="Tag->create(name: 'maths')" />
<do>$post->tags()->attach($maths)</do>
<do>$post->tags()->sync([$maths])</do>import KScript.Data
$maths = Tag->create(name: 'maths')
$post->tags()->attach($maths)
$post->tags()->sync([$maths])属于(belongs-to)关系直接设置在记录本身上。$post.author = $ben 设置 author_id,$post->save() 则将其写入。
条件中的关系
条件可以通过关系进行读取,工作由数据库完成:
<let name="busy" value="User->where(fn(u) => $u.posts->count() > 10)->get()" />
<let name="tagged" value="Post->where(fn(p) => $p.tags->any(fn(t) => $t.name == 'maths'))->get()" />
<let name="by_ada" value="Post->where(fn(p) => $p.author.name == 'Ada')->get()" />
<let name="no_profile" value="User->where(fn(u) => $u.profile == null)->get()" />$busy = User->where(fn(u) => $u.posts->count() > 10)->get()
$tagged = Post->where(fn(p) => $p.tags->any(fn(t) => $t.name == 'maths'))->get()
$by_ada = Post->where(fn(p) => $p.author.name == 'Ada')->get()
$no_profile = User->where(fn(u) => $u.profile == null)->get()在列表关系上,->count()、->any()、->is_empty()、->all()、->sum()、->average()、->min() 和 ->max() 会被转换为 SQL,并各自可带一个作用于相关记录的可选 lambda。对于单个相关记录,你可以读取其字段、将其与一条记录比较,或将其与 null 比较。与记录比较也支持简写形式:
<let name="mine" value="Post->where(author: $ada)->get()" />$mine = Post->where(author: $ada)->get()关系上的作用域
作用域也适用于关系,并且只作用于该关系的记录:$ada->posts()->live()->count() 只统计 Ada 未删除的帖子。