colinmollenhour / magento-mongo

MongoDb abstraction layer and atomic job queue for Magento
Other
124 stars 50 forks source link

[Question] Document with embedded document #19

Closed ntejapermana closed 9 years ago

ntejapermana commented 9 years ago

Hi Colin,

I'm using your magento-mongo extension in my project and it's awesome. But I have problem if my document has embedded document in it. It always throws 'Invalid embedded object instance for xxx'. Could you help me with this problem, please.

Thank you

colinmollenhour commented 9 years ago

The embedded object must be an instance of Cm_Mongo_Model_Abstract: https://github.com/colinmollenhour/magento-mongo/blob/d77b9d46b49abdec772b9d96b815bb3e7fbedb6f/code/Model/Resource/Abstract.php#L523

ntejapermana commented 9 years ago

Thank you for the response Colin. I have made the embedded object extends Cm_Mongo_Model_Abstract, the problem is this line of code always return NULL :

    $model = $object->getDataUsingMethod($field);

https://github.com/colinmollenhour/magento-mongo/blob/d77b9d46b49abdec772b9d96b815bb3e7fbedb6f/code/Model/Resource/Abstract.php#L522

colinmollenhour commented 9 years ago

Ahh, the intent is that the method exists and returns an object of the right type. So if your field name is foo_bar then your parent object model should have a method getFooBarwhich usually would just look like this:

  public function getFooBar()
  {
    return $this->_getEmbeddedObject('foo_bar');
  }

You would typically also have a setFooBar that uses the _setEmbeddedObject method.

Edit: referenced wrong method names..

colinmollenhour commented 9 years ago

Edited previous comment which referenced the wrong method names...

ntejapermana commented 9 years ago

Thank you for explanation Colin. It works like a charm.