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

How can I manually join 2 DataObjects?


Go to End


5 Posts   2529 Views

Avatar
andy_steel

Community Member, 31 Posts

2 March 2010 at 1:28am

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?

Avatar
dhensby

Community Member, 253 Posts

2 March 2010 at 1:42pm

Edited: 02/03/2010 2:18pm

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.

Avatar
andy_steel

Community Member, 31 Posts

2 March 2010 at 10:26pm

Edited: 02/03/2010 10:31pm

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.

Avatar
dhensby

Community Member, 253 Posts

3 March 2010 at 1:58am

Edited: 03/03/2010 1:58am

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

Avatar
andy_steel

Community Member, 31 Posts

3 March 2010 at 2:12am

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.