且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何建立多对多关系

更新时间:2023-02-15 18:03:31

在对象/api级别具有多对多关系,App Maker会提供一系列相关记录.假设我们有具有多对多关系的问题"和标签"模型.我们可以从关系的任何一端创建关联:

With many-to-many relation on the object/api level App Maker gives an array of related records. Let's say we have 'Questions' and 'Tags' models with many-to-many relation. We can create association from any end of the relation:

// create association from question side
question.Tags.push(tag);

// create association from tag side
tag.Questions.push(question);

Multiselect窗口小部件即可完成此工作.假设我们需要为问题添加一些标签,并且需要将multiselect绑定到数据库中所有的标签,然后绑定看起来与此类似

Multiselect Widget will do this work for. Let's say we need to add some tags for a questions and we need to bind multiselect to all tags we have in our DB then binding will look similar to this

// binding for Multiselect's names (.. - two dots mean projection)
@datasources.Tags.items..Name

// binding for Multiselect's options
@datasources.Tags.items

// binding for Multiselect's values
// assuming that parent widget is bound to datasource with question
// and `@datasource.item` is question
@datasource.item.Tags

带有建议框

with Suggest Box and Dropdown widgets binding will be similar to multiselect's one but you'll need to do some scripting:

// onValueEdit event handler
// assuming that parent widget is bound to datasource with question
widget.datasource.item.Tags.push(newValue);