如何以编程方式 link 新创建的记录到另一个 table 的记录
How to programmatically link newly created records to a record from another table
在此先感谢您的建议!
背景
我正在创建一个数据库来跟踪客户下的订单。
“Orders
”table 存储有关订单的一般详细信息,例如客户姓名、订单日期和交货要求日期。
单独的“Order_Items
”table 存储客户订购的特定商品。
'Orders
'table和'Order_Items
'table之间是一对多的关系,即一个'Order
' 可以有多个 'Order_Items
',但每个 'Order_Item
' 只能与一个 'Order
' 关联。
当前状态
目前,我有一个页面,用户可以在其中创建新的“Order
”记录。然后用户将被带到另一个页面,他们可以在其中创建订单所需数量的“Order_Item
”记录。
期望状态
我想实现的是:当用户创建新的'Order_Item
'记录时,它会自动分配当前'Order
'记录作为新'Order_Item
'记录的外键.
到目前为止我已经尝试过什么
用户手动操作:在“Order
”及其所有“Order_Items
之间建立link的一种方法' 将添加一个下拉小部件,它有效地询问用户 "Which order number do all of these items belong to"? 然后用户的操作将在两个 tables 并将一个 'Order
' 与许多 'Order_Items
' 相关联。但是,我的目标是以编程方式处理此步骤。
官方文档: 我提到 offical documentation which was useful, but as I'm still learning I don’t really know exactly what to search for. The prefetch 功能看起来很有希望,但实际上并没有建立 link;它只是更有效地加载相关记录。
App Maker 教程: 我找到了一个 App Maker tutorial,它创建了一个 HR 应用程序,用户可以在其中创建“Departments
”列表,然后创建一个列表'Employees
',然后link一个'Employee
'到一个'Department
'。但是,在示例应用程序中,此连接是由用户手动建立的。在我想要的状态下,我希望以编程方式建立 link。
手动保存模式:
我也试过切换到 manual save mode,这样用户必须创建一个草稿“Orders
”记录,然后创建几个草稿“Order Items
”记录,然后一次保存它们。但是,我还没有成功完成这项工作。我不确定这种方法的失败是否是因为 1) 我试图在多个 table 上创建草稿记录,2) 我只是没有正确地做,或者 3) 我以为我在某处读到草稿记录已被弃用。
其他想法
我是这个领域的新手,可能是错误的,但我觉得我可能需要使用一些脚本来建立 link。例如,也许我可以使用全局变量来记住用户创建的“Order
”。然后,对于每个“Order_Item
”,我可以使用 onBeforeCreate
事件触发一个脚本,该脚本在“Order_Item
”和“Order
”之间建立 link ' 这是从先前建立的全局变量中记住的。
更新问题
感谢 Markus 和 Morfinismo 的回答。我一直在使用这两个答案并取得了一些成功。
Morfinismo:我已经成功地在现有记录上使用了您指示我使用的代码,但似乎无法将其用于新创建的记录。
例如:
widget.datasource.createItem(); // This creates a new record
var managerRecord = app.datasources.Manager.item; // This sets the Manager of the currently selected parent record as a variable successfully.
var teamRecord = app.datasources.Teams.item; // This attempts to set the Manager of the currently selected record as a variable. However, the record that was created in line 1 is not selected. Therefore, App Maker does not seem to know which record this line of code relates to and returns the error Cannot set property ‘Manager’ of null.
// Assign the manager to the team.
teamRecord.Manager = managerRecord; // This successfully assigns the manager but only in cases where the previous line of code was successful (i.e. existing records and not newly created ones).
对于如何将此代码应用于由第 1 行的初始代码行创建的记录,您有任何建议或意见吗?
我在您的问题下发表的评论包括 link 到 official documentation 来解释您的需求。无论如何,您的问题不够清楚,无法确定您是通过客户端脚本还是服务器脚本创建记录,因此这是一个非常笼统的答案。
通过client script管理关系:
var managerRecord = app.datasources.Manager.item;
var teamRecord = app.datasources.Teams.item;
// Assign the manager to the team.
teamRecord.Manager = managerRecord;
// Changes are saved automatically if the datasource in auto-save mode
// Add a team member to a Manager's team.
// Note: Retrieve Members on the client before proceeding, such as by using prefetch option in datasource - datasources Team -> Members)
var engineerRecord = app.datasources.TeamMember.item;
teamRecord.Members.push(engineerRecord);
通过server script管理关系:
// Get the record for the Team to modify.
var teamRecord = app.models.Teams.getRecord("team1");
// Assign a manager to the Team.
var managerRecord = app.models.EmployeeDB.getRecord("manager1");
teamRecord.Manager = managerRecord;
// Note: The new association is not saved yet
// Assign a team member to the Team.
var engineerRecord = app.models.EmployeeDB.getRecord("engineer1");
teamRecord.Members.push(engineerRecord);
// Save both changes to the database.
app.saveRecords([teamRecord]);
以上信息直接取自官方文档,就像我说的,我在你的问题下的评论中提到了。
我发现为像您这样的情况创建相关项目的最简单方法是实际导入一个数据源设置为 Parent: Child (relation)
或 Parent: Child (relation) (create)
的表单。因此,在您的情况下,数据源需要设置为 Order: Order_Items (relation)
.
您可以使用表单小部件向导以两种不同的方式完成此操作:
选项 1:
- 如果您的页面数据源设置为 Order_Items,请将表单拖到页面上。
- 在数据源 selection 部分,表单小部件中的数据源应默认为“继承:Order_Items”。单击左下角的 'Advanced' 按钮,然后从数据源类别中找到 Order 作为您的数据源,然后在下一个字段中选择 select 关系,然后在下一个字段中选择 Order_Items,选择'Insert only' 或 'Edit' 表单,然后在表单中添加您想要的相应字段。
- 现在,在该表单中创建的每个项目都将自动成为订单数据源中当前 selected 记录的子记录。
选项 2:
- 如果您的页面数据源设置为订单,请将您的表单拖到页面上。
- 在数据源 selection 部分,表单小部件中的数据源应默认为
Inherited: Order
。在数据源 selection 部分向下滚动,直到找到 Order: Order_Items (relation)
,然后选择 'Insert only' 或 'Edit' 表单,然后选择表单中所需的适当字段。
- 现在,在该表单中创建的每个项目都将自动成为订单数据源中当前 selected 记录的子记录。
在您的 Order 模型中,确保正确设置了安全设置,允许用户在 Order 中创建 Order_Items 关系。在我看来,这是最简单的方法,因为您不必将父项硬编码到您的表单或 client/server 脚本中。它自动基于当前 selected 父级,并且本质上与@Morfinismo 在客户端脚本部分中解释的相同。
在此先感谢您的建议!
背景 我正在创建一个数据库来跟踪客户下的订单。
“Orders
”table 存储有关订单的一般详细信息,例如客户姓名、订单日期和交货要求日期。
单独的“Order_Items
”table 存储客户订购的特定商品。
'Orders
'table和'Order_Items
'table之间是一对多的关系,即一个'Order
' 可以有多个 'Order_Items
',但每个 'Order_Item
' 只能与一个 'Order
' 关联。
当前状态
目前,我有一个页面,用户可以在其中创建新的“Order
”记录。然后用户将被带到另一个页面,他们可以在其中创建订单所需数量的“Order_Item
”记录。
期望状态
我想实现的是:当用户创建新的'Order_Item
'记录时,它会自动分配当前'Order
'记录作为新'Order_Item
'记录的外键.
到目前为止我已经尝试过什么
用户手动操作:在“Order
”及其所有“Order_Items
之间建立link的一种方法' 将添加一个下拉小部件,它有效地询问用户 "Which order number do all of these items belong to"? 然后用户的操作将在两个 tables 并将一个 'Order
' 与许多 'Order_Items
' 相关联。但是,我的目标是以编程方式处理此步骤。
官方文档: 我提到 offical documentation which was useful, but as I'm still learning I don’t really know exactly what to search for. The prefetch 功能看起来很有希望,但实际上并没有建立 link;它只是更有效地加载相关记录。
App Maker 教程: 我找到了一个 App Maker tutorial,它创建了一个 HR 应用程序,用户可以在其中创建“Departments
”列表,然后创建一个列表'Employees
',然后link一个'Employee
'到一个'Department
'。但是,在示例应用程序中,此连接是由用户手动建立的。在我想要的状态下,我希望以编程方式建立 link。
手动保存模式:
我也试过切换到 manual save mode,这样用户必须创建一个草稿“Orders
”记录,然后创建几个草稿“Order Items
”记录,然后一次保存它们。但是,我还没有成功完成这项工作。我不确定这种方法的失败是否是因为 1) 我试图在多个 table 上创建草稿记录,2) 我只是没有正确地做,或者 3) 我以为我在某处读到草稿记录已被弃用。
其他想法
我是这个领域的新手,可能是错误的,但我觉得我可能需要使用一些脚本来建立 link。例如,也许我可以使用全局变量来记住用户创建的“Order
”。然后,对于每个“Order_Item
”,我可以使用 onBeforeCreate
事件触发一个脚本,该脚本在“Order_Item
”和“Order
”之间建立 link ' 这是从先前建立的全局变量中记住的。
更新问题
感谢 Markus 和 Morfinismo 的回答。我一直在使用这两个答案并取得了一些成功。
Morfinismo:我已经成功地在现有记录上使用了您指示我使用的代码,但似乎无法将其用于新创建的记录。
例如:
widget.datasource.createItem(); // This creates a new record
var managerRecord = app.datasources.Manager.item; // This sets the Manager of the currently selected parent record as a variable successfully.
var teamRecord = app.datasources.Teams.item; // This attempts to set the Manager of the currently selected record as a variable. However, the record that was created in line 1 is not selected. Therefore, App Maker does not seem to know which record this line of code relates to and returns the error Cannot set property ‘Manager’ of null.
// Assign the manager to the team.
teamRecord.Manager = managerRecord; // This successfully assigns the manager but only in cases where the previous line of code was successful (i.e. existing records and not newly created ones).
对于如何将此代码应用于由第 1 行的初始代码行创建的记录,您有任何建议或意见吗?
我在您的问题下发表的评论包括 link 到 official documentation 来解释您的需求。无论如何,您的问题不够清楚,无法确定您是通过客户端脚本还是服务器脚本创建记录,因此这是一个非常笼统的答案。
通过client script管理关系:
var managerRecord = app.datasources.Manager.item;
var teamRecord = app.datasources.Teams.item;
// Assign the manager to the team.
teamRecord.Manager = managerRecord;
// Changes are saved automatically if the datasource in auto-save mode
// Add a team member to a Manager's team.
// Note: Retrieve Members on the client before proceeding, such as by using prefetch option in datasource - datasources Team -> Members)
var engineerRecord = app.datasources.TeamMember.item;
teamRecord.Members.push(engineerRecord);
通过server script管理关系:
// Get the record for the Team to modify.
var teamRecord = app.models.Teams.getRecord("team1");
// Assign a manager to the Team.
var managerRecord = app.models.EmployeeDB.getRecord("manager1");
teamRecord.Manager = managerRecord;
// Note: The new association is not saved yet
// Assign a team member to the Team.
var engineerRecord = app.models.EmployeeDB.getRecord("engineer1");
teamRecord.Members.push(engineerRecord);
// Save both changes to the database.
app.saveRecords([teamRecord]);
以上信息直接取自官方文档,就像我说的,我在你的问题下的评论中提到了。
我发现为像您这样的情况创建相关项目的最简单方法是实际导入一个数据源设置为 Parent: Child (relation)
或 Parent: Child (relation) (create)
的表单。因此,在您的情况下,数据源需要设置为 Order: Order_Items (relation)
.
您可以使用表单小部件向导以两种不同的方式完成此操作:
选项 1:
- 如果您的页面数据源设置为 Order_Items,请将表单拖到页面上。
- 在数据源 selection 部分,表单小部件中的数据源应默认为“继承:Order_Items”。单击左下角的 'Advanced' 按钮,然后从数据源类别中找到 Order 作为您的数据源,然后在下一个字段中选择 select 关系,然后在下一个字段中选择 Order_Items,选择'Insert only' 或 'Edit' 表单,然后在表单中添加您想要的相应字段。
- 现在,在该表单中创建的每个项目都将自动成为订单数据源中当前 selected 记录的子记录。
选项 2:
- 如果您的页面数据源设置为订单,请将您的表单拖到页面上。
- 在数据源 selection 部分,表单小部件中的数据源应默认为
Inherited: Order
。在数据源 selection 部分向下滚动,直到找到Order: Order_Items (relation)
,然后选择 'Insert only' 或 'Edit' 表单,然后选择表单中所需的适当字段。 - 现在,在该表单中创建的每个项目都将自动成为订单数据源中当前 selected 记录的子记录。
在您的 Order 模型中,确保正确设置了安全设置,允许用户在 Order 中创建 Order_Items 关系。在我看来,这是最简单的方法,因为您不必将父项硬编码到您的表单或 client/server 脚本中。它自动基于当前 selected 父级,并且本质上与@Morfinismo 在客户端脚本部分中解释的相同。