21309 Posts in 5738 Topics by 2603 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1531 Views |
-
How can I manually join 2 DataObjects?

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?
-
Re: How can I manually join 2 DataObjects?

2 March 2010 at 1:42pm Last edited: 2 March 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.
-
Re: How can I manually join 2 DataObjects?

2 March 2010 at 10:26pm Last edited: 2 March 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.
-
Re: How can I manually join 2 DataObjects?

3 March 2010 at 1:58am Last edited: 3 March 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];
} -
Re: How can I manually join 2 DataObjects?

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.
| 1531 Views | ||
|
Page:
1
|
Go to Top |


