21293 Posts in 5733 Topics by 2602 members
| Go to End | Next > | |
| Author | Topic: | 1545 Views |
-
ComponentSet relationships

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.
-
Re: ComponentSet relationships

15 September 2010 at 12:44am Last edited: 15 September 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);
} -
Re: ComponentSet relationships

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.
-
Re: ComponentSet relationships

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:
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?
-
Re: ComponentSet relationships

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?
-
Re: ComponentSet relationships

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.
-
Re: ComponentSet relationships

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. -
Re: ComponentSet relationships

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.
| 1545 Views | ||
| Go to Top | Next > |


