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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

ComponentSet relationships


Go to End


10 Posts   2772 Views

Avatar
dendeffe

Community Member, 135 Posts

14 September 2010 at 10:02am

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.

Avatar
swaiba

Forum Moderator, 1899 Posts

15 September 2010 at 12:44am

Edited: 15/09/2010 2:32am

Hi,

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
		$orderComponentSet->addMany(array($productID));
		
		//add with dataobject
		$dataobject = DataObject::get_by_id('Order',productID);
		$orderComponentSet->add($dataobject);
	}

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 2:29am

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.

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 7:05am

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:

http://skitch.com/dendeffe/dsimj/macbookdb.local-localhost-ssshop-order-productpages-phpmyadmin-3.2.4

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:

http://skitch.com/dendeffe/dsimi/macbookdb.local-localhost-ssshop-order-productpages-phpmyadmin-3.2.4

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

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 9:12am

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?

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 9:25am

So,

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.

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 9:46am

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.

Avatar
dendeffe

Community Member, 135 Posts

15 September 2010 at 7:23pm

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