apply

apply(self, connection=None, params=None, safe=False):

使用范围: server

编程语言: python

父类: Item 类

描述说明

将所有已更新、已插入、已删除的记录,从实体项数据集中写入到服务端的数据库中。

apply 方法:

  • 检查实体项是否是明细项,如果是,则返回(明细项的主控表将会保存其更改)

  • 检查实体项是否处于 “编辑” 或 “插入” state , 如果是,则提交记录。

  • 检查变更日志是否有变更,如果没有,则返回。

  • 触发为实体项已定义的 on_before_apply 事件处理程序。

  • 如果 connection 参数是 None ,将调用任务的 connect 方法从任务连接池中获取一个连接。

  • 如果为任务定义了 on_apply 事件处理程序,则执行它。

  • 如果为实体项定义了 on_apply 事件处理程序,则执行它。

  • 使用数据库连接,生成并执行 SQL 查询语句,以向数据库写入更改。

  • 如果未指定 connection 参数,那么将变更提交到数据库,并将连接归还至连接池。

  • 在将变更写入数据库之后,更新变更日志和实体项数据集 - 更新新记录的主键值

  • 触发为实体项已定义的 on_after_apply 事件处理程序。

参数

  • connection - 如果指定了该参数,应用程序将使用它来执行生成的 SQL 查询 (不会提交变更,也不会关闭连接);否则,应用程序会从任务连接池获取一个连接, 并在提交变更后将该连接归还给连接池。

  • params - 使用该参数传递用户的自定义选项,供 on_apply 事件处理器使用。此参数必须是一个键值对对象。

  • safe - 如果设置为 True,该方法会检查调用此方法的用户是否具备在当前项的数据库表中创建、编辑或删除记录的权限(即将执行此类操作之前);若用户无此权限,则抛出异常。该参数的默认值为 False 。 参考 角色

Examples

下面的第二个示例中,在一个事务中保存变更。

def change_invoice_date(item, item_id):
    inv = item.copy()
    cust = item.task.customers.copy()
    inv.set_where(id=item_id)
    inv.open()
    if inv.record_count():
        now = datetime.datetime.now()
        cust.set_where(id=inv.customer.value)
        cust.open()

        inv.edit()
        inv.invoice_datetime.value = now
        inv.post()
        inv.apply()

        cust.edit()
        cust.last_action_date.value = now
        cust.post()
        cust.apply()
def change_invoice_date(item, item_id):
    con = item.task.connect()
    try:
        inv = item.copy()
        cust = item.task.customers.copy()
        inv.set_where(id=item_id)
        inv.open()
        if inv.record_count():
            now = datetime.datetime.now()
            cust.set_where(id=inv.customer.value)
            cust.open()

            inv.edit()
            inv.invoice_datetime.value = now
            inv.post()
            inv.apply(con)

            cust.edit()
            cust.last_action_date.value = now
            cust.post()
            cust.apply(con)
    finally:
        con.commit()
        con.close()

另请参见

修改数据集