简体   繁体   English

ActiveJdbc更新联接表属性

[英]ActiveJdbc update join-table attribute

Good evening, I am trying to update a join-table attribute for existing records. 晚上好,我正在尝试更新现有记录的联接表属性。 I have two models, one called Records , the other called Shoppingcarts . 我有两个模型,一个叫做Records ,另一个叫做Shoppingcarts These are connected by a join table called records_shoppingcarts . 这些通过称为records_shoppingcarts联接表连接。 I added a relationship attribute to the join table called record_amount which is supposed to hold the amount of each record in the join table. 我在record_amount表中添加了一个名为record_amount的关系属性,该属性应该保存record_amount表中每条记录的数量。 At the moment, whenever an item is added to a shoppingcart, only one item is added. 目前,每当将一项商品添加到购物车时,只会添加一项。 The user is then able to modify the amount of items in the shoppingcart in their checkout view. 然后,用户可以在其结帐视图中修改购物车中的商品数量。 I am handling this by querying the join table records_shoppingcarts for entries with the corresponding record_id and updating the column record_amount to the new amount of records in the shoppingcart. 我正在通过查询联接表records_shoppingcarts中具有相应record_id的条目,并将record_amount列更新为record_amount中新的记录数量来处理此问题。 This looks something linke that: 看起来有些联系:

List<RecordsShoppingcarts> associations = RecordsShoppingcarts.where("shoppingcart_id = ?", s.get("id"));

            //update the number of records in the join table
            for(RecordsShoppingcarts rs : associations) {                    

                if((int)rs.getInteger("record_id") == (int)rec.getInteger("id")) {
                    rs.delete(); //cleanup
                    rs.set("record_amount", amount);
                    rs.saveIt();                        

                }
            }                
        }

Unfortunately though, whenever saveIt() gets called; 不幸的是,每当saveIt()时; the entry isn't updated in the database but a new entry is created with the same shoppingcart_id , recordId and the updated amount. 该条目不会在数据库中更新,而是使用相同的shoppingcart_idrecordId和更新的金额创建一个新条目。 I am probably missing something here, since I can't imagine manually updating join tables is intended. 我可能在这里丢失了一些东西,因为我无法想象要手动更新联接表。 Unfortunately though, I couldn't find anything on updating relationship attributes in join tables. 但是,不幸的是,在更新联接表中的关系属性时,我找不到任何东西。 Is this even provided by ActiveJdbc? 这甚至是ActiveJdbc提供的吗?

Any help would be much appreciated. 任何帮助将非常感激。 Best wishes, derelektrischemoench 最好的祝福,derelektrischemoench

The framework is working exactly as expected. 该框架完全按预期工作。 You need to familiarize yourself with how it decides to create a new record or update an existing one by reading this page: http://javalite.io/surrogate_primary_keys 您需要通过阅读以下页面来熟悉如何决定创建新记录或更新现有记录: http : //javalite.io/surrogate_primary_keys

Basically, you are deleting a record by: 基本上,您将通过以下方式删除记录:

 rs.delete();

so, next time you do: 因此,下次您执行以下操作:

rs.saveIt();   

it creates a new record. 它创建一个新记录。 Simply remove the rs.delete(); 只需删除rs.delete(); and you will be fine. 你会没事的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM