Jump to:

22979 Posts in 11808 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » How can I manually join 2 DataObjects?

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
Go to End
Author Topic: 1692 Views
  • andy_steel
    Avatar
    Community Member
    31 Posts

    How can I manually join 2 DataObjects? Link to this post

    I've tried doing the following, but it does not work.

    $products = DataObject::get('Product'); // Product has a has_one relationship to Image
    $images= DataObject::get('Image');

    foreach ($products as $product) {
       
       foreach ($images as $image) {
          
          if ($product->ImageID == $image->ID) {
             
             $product->setComponent('Image', $image);
             break;
          }
       }
    }

    // even though an image has been assigned, it still queries the database.
    $products->First()->Image();

    Has anyone else had this issue?

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: How can I manually join 2 DataObjects? Link to this post

    Hey Andy,

    What debugging have you done? Does $products->First()->getComponent('Image') return it?

    I had a dig in the DataObject class and what you're doing does seem right; although I cant find where the Image() function is created and thus whether it is likely to call getComponent().

    Ok, i did some more digging; i think that its a bit more complex than just setting components. You might be able to just use the getComponent method as a quick fix, but you might need to seriously investigate how the DataObject class works. Looks like you might need to addWrapperMethod() or createMethod() on the object. It seems that defineMethods() is only called on instantiation of the product data object, rather than on a call to a method.

    Hope that helps.

  • andy_steel
    Avatar
    Community Member
    31 Posts

    Re: How can I manually join 2 DataObjects? Link to this post

    Hi Pigeon,

    Thanks for looking into this. I've had a closer look at DataObject and I think the reason why its not returning an object is because an MD5 sum is used to take into account joins, filters, limits etc. Since no MD5 is added when adding the object via setComponent(), getComponent() doesn't work since it calculates an MD5 sum.

    I'll report this as a bug on the issue tracker.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: How can I manually join 2 DataObjects? Link to this post

    Hi Andy,

    I dont think that's right. getComponents() appends a MD5 string, but getComponent() doesn't.

    DataObject.php Line 1036:

    public function getComponent($componentName) {
          if(isset($this->components[$componentName])) {
             return $this->components[$componentName];
          }

  • andy_steel
    Avatar
    Community Member
    31 Posts

    Re: How can I manually join 2 DataObjects? Link to this post

    I've managed to fix it by adding the following lines 3 lines to getComponent() in DataObject.php:

       public function getComponent($componentName) {
          
          if(isset($this->componentCache[$componentName])) {
             return $this->componentCache[$componentName];
          }

    It wasn't checking the componentCache array.

    1692 Views
Page: 1
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.