我正在开发一个安静的应用程序,它在后面使用 Clojure,在前面使用 Angular。我有以下表格:客户、订单、项目、订单项。我正在使用 korma 与 MySql 数据库进行通信。当我对所有实体进行 CRUD 操作时,一切正常。但我不知道如何在数据库中插入多行?我应该在 korma 中使用交易吗?
(declare customer)
(declare customerorder)
(declare item)
(declare orderitems)
(schema/defschema OrderItem
{
:OrderItemId schema/Int
:OrderId schema/Int
:ItemId schema/Int
:Quantity schema/Int
})
(schema/defschema Customer
{
:CustomerId schema/Int
:Name schema/Str
:Contact schema/Str
})
(schema/defschema UpdateCustomer
{
:Name schema/Str
:Contact schema/Str
})
(schema/defschema NewCustomer
{
:Name schema/Str
:Contact schema/Str
})
(schema/defschema Order
{
:OrderId schema/Int
:CustomerId schema/Int
:PMethod schema/Str
:GTotal schema/Num
})
(schema/defschema Item
{
:ItemId schema/Int
:Name schema/Str
:Price schema/Num
})
(defentity customer
(pk :CustomerId)
(has-many customerorder {:fk :CustomerId})
(entity-fields :CustomerId :Name :Contact))
(defentity customerorder
(pk :OrderId)
(has-many orderitems {:fk :OrderId})
(belongs-to customer {:fk :CustomerId})
(entity-fields :PMethod :GTotal :OrderId)
)
(defentity item
(pk :ItemId)
(entity-fields :ItemId :Name :Price)
(has-many orderitems {:fk :ItemId}))
(defentity orderitems
(pk :OrderItemId)
(belongs-to item {:fk :ItemId})
(belongs-to customerorder {:fk :OrderId})
(entity-fields :OrderItemId :ItemId :Quantity))
这是我的查询,用于选择使用现金支付订单和订单商品的客户:
(defn get-customersbypmethod [PMethod]
(select customerorder (with customer (with orderitems)) (where {:PMethod PMethod})))
我的问题是如何插入带有订单商品的订单?