Hibernate 什么';这是清除列表以便为ColdFusion 9表单重新添加元素的最佳方法吗?
假设我有一个Hibernate 什么';这是清除列表以便为ColdFusion 9表单重新添加元素的最佳方法吗?,hibernate,orm,coldfusion,many-to-many,coldfusion-9,Hibernate,Orm,Coldfusion,Many To Many,Coldfusion 9,假设我有一个商店和一个产品实体,它们之间有多对多的关系,两者都是持久的CFC 我想使用表单帖子中的产品ID列表更新现有商店的产品,这意味着我必须首先删除所有产品。最好的方法是什么?到目前为止,我已经尝试使用了ArrayNew(1): store=entityLoadByPK(“store”,FORM.id); //通过将产品分配到新阵列来清除产品 store.setProducts(ArrayNew(1)); //重新添加产品 for(productid中的id) { 产品=实体负载(“产品”
商店
和一个产品
实体,它们之间有多对多的关系,两者都是持久的CFC
我想使用表单帖子中的产品ID列表更新现有商店的产品,这意味着我必须首先删除所有产品。最好的方法是什么?到目前为止,我已经尝试使用了ArrayNew(1)
:
store=entityLoadByPK(“store”,FORM.id);
//通过将产品分配到新阵列来清除产品
store.setProducts(ArrayNew(1));
//重新添加产品
for(productid中的id)
{
产品=实体负载(“产品”,id);
储存。添加产品(产品);
}
实体保存(存储);
我尝试使用循环移除产品:
<cfscript>
store = entityLoadByPK("Store", FORM.id);
// clear the products by removing them with a loop
for (product in store.getProducts())
{
store.removeProduct(product);
}
// re-add the products
for (id in productid)
{
product = entityLoad("Product", id);
store.addProduct(product);
}
entitySave(store);
</cfscript>
store=entityLoadByPK(“store”,FORM.id);
//通过使用环移除产品来清除产品
对于(商店中的产品。getProducts())
{
储存、移除产品(产品);
}
//重新添加产品
for(productid中的id)
{
产品=实体负载(“产品”,id);
储存。添加产品(产品);
}
实体保存(存储);
这两种方法都很好,但有更好的处理方法吗?或者这不要紧,而且两者在幕后(即在SQL中)会做相同的事情吗?在store.cfc
中添加这些函数。或者,首先检查变量.products
是否存在
void function clearProducts()
{
arrayClear(variables.products);
}
void function addProducts(required Array ps)
{
for (var p in ps)
ArrayAppend(variables.products, p);
}
然后在控制器层
var hql = "from Product where id in (:productIDs)";
var products = ormExecuteQuery(hql, {productIDs=Form.productID});
if (!store.hasProduct())
store.setProducts(products);
else
{
store.clearProducts();
store.addProducts(products);
}
谢谢,这是一个非常好的解决方案!:)很高兴你喜欢。投赞成票!
var hql = "from Product where id in (:productIDs)";
var products = ormExecuteQuery(hql, {productIDs=Form.productID});
if (!store.hasProduct())
store.setProducts(products);
else
{
store.clearProducts();
store.addProducts(products);
}