Jump to:

22977 Posts in 11747 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » ComponentSet relationships

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

Page: 1 2
Go to End
Author Topic: 1782 Views
  • dendeffe
    Avatar
    Community Member
    135 Posts

    ComponentSet relationships Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: ComponentSet relationships Link to this post

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

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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.

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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?

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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?

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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.

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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.

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: ComponentSet relationships Link to this post

    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.

    1782 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.