In simple terms it's an inventory system, that is tied to SKU's. The SKU is a composite of items, sometimes multiples of the exact same item.
E.G
SKU for a dining room set would be, 4(Item)"chair" and 1(Item)"table". If I could get GORN to recognize it's tied to multiple instances of "chair", so I can keep count.
Here's the basic code as I've tried
class Item implements Serializable{
String name;
String description;
}
class Sku {
String description;
String skuCode;
Collection items;
static hasMany = [items:Item];
}
//… GENERATE TEST DATA
item = new Item(name:"Table", description:"Oak table");
item.save();
item = new Item(name:"Chair", description:"Oak chair");
item.save();
sku= new Sku(description:"A complete dining room set for 4");
sku.items = [Item.findByName("Table"),Item.findByName("Chair"),
Item.findByName("Chair"),Item.findByName("Chair"),
Item.findByName("Chair")];
sku.save();
This will only show the SKU as being of "Table" and "Chair".
I'm not even sure how to do this in SQL. But I would like to know if there's a construct that can accomplish what I want. This seems like it would be a clean way to code it.
The end goal is a system that SKU will contain many different item combos, and I'll want to be able to narrow down possible SKU's by entering item combo's.
E.G If I search for Table + 1 chair, I would see Table & 4Chairs and table + 2 chairs (after it's inserted) etc. Or Table+Chair(s)+Island+Stools(s)+Fridge
Thanks
This took a little digging into documentation to figure it out.
Zoidberg is right that GORM defaults to a set. I had used Collection as my understanding was, that was an unordered list that could contain duplicates. The documentation lead me to try a List. List allows duplicates. It does make a mention of performance about using lists, so read that part too. That was my reasoning for using Collection at first, but that does not seem to work as I need it.
The other issue is how to query based on a partial list. E.G Return all SKU's that have the item "Chair" present somewhere.
This SO answer ended up being my solution to that piece.
So here is my example code as it stands.
class Item implements Serializable{
String name;
String description;
}
class Sku {
String description;
String skuCode;
List items;
static hasMany = [items:Item];
}
//… GENERATE TEST DATA
item = new Item(name:"Table", description:"Oak table");
item.save();
item = new Item(name:"Chair", description:"Oak chair");
item.save();
sku= new Sku(description:"A complete dining room set for 4");
sku.items = [Item.findByName("Table"),Item.findByName("Chair"),
Item.findByName("Chair"),Item.findByName("Chair"),
Item.findByName("Chair")];
sku.save();
//Query test data
Sku.executeQuery("FROM Sku as s WHERE :item in elements(s.items)",
[item: Item.findByName("Chair")])
//returns List<Sku> of any Sku's that contain the item "chair".
//I need to create a version that takes a list, so I can narrow the search.
This issue in Jira with GORM is very similar to the one I get if I attempt to use findByItemsInList, or any other findBy*.