Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

ComponentSet relationships

Go to End

10 Posts   1932 Views


14 September 2010 at 10:02am Community Member, 135 Posts

I'm trying to get my head around the inner workings of DataObjects at the moment. I have set up this:

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.


15 September 2010 at 12:44am (Last edited: 15 September 2010 2:32am), Forum Moderator, 1796 Posts


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);


15 September 2010 at 2:29am Community Member, 135 Posts

Yes, adding a product to the order is what I’m trying to do. I’ll check if addMany does the trick tonight. Thanks for the help.


15 September 2010 at 7:05am Community Member, 135 Posts

Hmmm I don’t see a difference from the add function. The first time, I get a record on my Order_ProductPages table, linking the two together:

I'd think by adding the same product to the order again, I'd get another row, but instead it is replaced by a row with another ID:

I'm looking for a way to add multiple copies of the ProductPageID to OrderID. Is this the best way of going about it?


15 September 2010 at 9:12am Community Member, 135 Posts

OK, I think I’m not using it correctly. In the loadChildIntoDatabase function in ComponentSet, there is this on line 149 to delete the existing row:

DB::query( "DELETE FROM \"$this->tableName\" WHERE \"$parentField\" = {$this->ownerObj->ID} AND \"$childField\" = {$item->ID}" );

If I comment that out, it works as I expect, I get another entry with the same connection. But that’s obviously not the way to make this work. So I might be doing it wrong.

Maybe I should just use DB::query directly to add the row (instead of using ComponentSet->add). Is that a good way of doing it?


15 September 2010 at 9:25am Community Member, 135 Posts


DB::query("INSERT INTO Order_ProductPages(OrderID, ProductPageID) VALUES ($this->ID, $productID)");

does insert it the way I was expecting. Still not sure if it’s a good way of doing it, though.


15 September 2010 at 9:46am Community Member, 135 Posts

OK, now I'm quite sure I’m doing it wrong :) I have multiple connections, but the DataObject I get for the current order only returns one of them.


15 September 2010 at 7:23pm Community Member, 135 Posts

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.

Go to Top