I'm trying to get my head around the inner workings of DataObjects at the moment. I have set up this: http://pastie.org/1156856
When I try to use my addProduct function, it works. A new record is created in the Order_ProductPages table. Whenever I call the function again to try and make the same connection between the order and the product, the previous record is overwritten.
I’m obviously missing something basic here. Or I’m trying to use ComponentSet for something it isn’t meant for.
Can anyone point me in the right direction? Thanks.
I found the code a little confusing as it is not clear what is going on, are you adding a relation to order or product page. If I assume you are relating the current page to an order (that can be found with $productID) this I recommend one of two ways - either by id or by dataobject (a bit clearer when the variable could mean many things)...
function addProduct($productID = 0)
$orderComponentSet = $this->Orders();
//add directly with ID
//add with dataobject
$dataobject = DataObject::get_by_id('Order',productID);
Browsing through the existing Ecommerce module, I see they use an extra Class Order_Item. I probably need to do something similar and make a connection between Order and Order_Item instead of Order and Product. That way I can always make a new Order_Item.