我计划仅将Ecto
的{{1}}和Schema
用于验证,而不将任何内容持久化到数据库中并试图弄清楚是否应该使用Ecto.Schema.schema/2
或Ecto.Schema.embedded_schema/1
。根据文档,它们之间的唯一区别是“ 嵌入式模式不需要源名称,并且不包含元数据字段。”
所以我选择了Changeset
,表现出色,但是让我怀疑元数据的确切用途是什么? Ecto.Schema.Metadata
docs对澄清这一点没有多大帮助:
存储结构的元数据。
字段是:
- state-结构寿命中的状态,是:build,:loaded, :已删除
- source-查询旁边的模式的源 前缀,默认为{nil,“ source”}
- context-由上下文存储的上下文 数据库
搜索“ 元”不会产生任何结果,并且“元数据”会在Ecto.Schema
docs中返回一个结果,这与上面引述的{{1} }。
更新
忘记了Ecto 3即将发布,而Hexdocs文档仍然适用于Ecto 2.2.11。在源代码中发现了the updated Metadata
docs,但更为冗长:
embedded_schema/1
({The updated Schema
docs也解决了我的上述困境:
embedded_schema/1
)
答案 0 :(得分:1)
<form [formGroup]="form">
template
is_home
translatable
<label for="template">Template</label>
<input type="text" id="template" formControlName="template">
<br><br>
<label for="is_home">Is Home</label>
<input type="text" formControlName="is_home" id="is_home">
<br><br>
<h1>translatable</h1>
<div formArrayName="translatable">
<div *ngFor="let group of localeFormArray; let i = index;">
<div [formGroupName]="i">
<label
[for]="'title'+supportedLocales[i].lang.locale">
{{ translateField('page::pages.title') }}
</label>
<input
formControlName="title"
[id]="'title'+supportedLocales[i].lang.locale"
class="form-control">
<br><br>
<label
[for]="'title'+supportedLocales[i].lang.locale">
{{ translateField('page::pages.title') }}
</label>
<input
formControlName="body"
[id]="'title'+supportedLocales[i].lang.locale"
class="form-control">
</div>
</div>
</div>
</form>
仅用于存储所有与数据库有关的信息。
何塞在series of posts on Ecto 2 → 3中提到,
自Ecto 2.0以来,越来越多的开发人员和团队一直在使用Ecto进行数据映射和验证,而无需数据库。但是,将Ecto添加到您的应用程序中仍然会带来很多SQL负担,例如适配器,沙箱和迁移,许多人认为这是混合消息。
后者与元数据有关。
Ecto 2有一个经验法则:是否需要后面的数据库表,请使用import { Component } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators, FormArray } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
form: FormGroup;
supportedLocales = [
{ lang: { locale: 'en-US' } },
{ lang: { locale: 'en-FR' } },
];
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.form = this.fb.group({
template: ['default', [Validators.required]],
is_home: [0],
translatable: this.fb.array(this.supportedLocales.map(locale => this.getFormGroupForLocale(locale)))
});
}
private getFormGroupForLocale(language) {
return this.fb.group({
title: [language.lang.locale + 'Title', [Validators.required]],
body: [language.lang.locale+'Body', [Validators.required]]
});
}
...
get localeFormArray() {
return (<FormArray>this.form.get('translatable')).controls;
}
}
;否则,请使用Ecto.Schema.Metadata
。
边注:我的一般建议是,当您希望简而言之理解某些内容时,不要阅读read the code文档。
答案 1 :(得分:0)
Ecto内部使用__meta__
字段来维护有关记录,关联(如果已加载,陈旧或更多)的元数据。
您链接的文档中的描述似乎是自给自足的:
存储结构的元数据。
字段是:
state
-结构生命周期中的状态,:built
之一,:loaded
,:deleted
source
-模式的源以及查询前缀, 默认为{nil, "source"}
context
-数据库存储的上下文