5

Collections provide a handy means of getting a blank model - e.g.:

$quote->getItemsCollection()->getNewEmptyItem();

Is there any means of doing this from an existing model without having to call for the collection - in other words directly from the instance? Something like this:

$product = Mage::getModel('catalog/product')->load(43);
$productNew = $product->getNewEmptyItem();

I do realize I could call getCollection() on the product first - not sure if this has some overhead.

Edit:

Obviously $product = Mage::getModel('catalog/product'); produces the desired result. Obviously. What I'm looking for is a means of getting a blank object without reinstantiating the model.

Perhaps I just need to switch to using singletons to ease my mind.

  • What's the need? – benmarks Mar 5 '13 at 23:37
  • I'm trying to create dynamic observers that won't necessarily know what the model is before it's retrieved - want a way of getting a blank instance of it without having to reinstantiate. I might be on the wrong track, but this is really handy to know regardless; albeit a #lazyweb way of getting this information! – philwinkle Mar 6 '13 at 3:51
  • I ask because re-instantiation is like nothing in terms of processing overhead. And, what are you doing with "dynamic observers"? – benmarks Mar 6 '13 at 13:07
  • 1
    Creating a frontend UI to attach observers to events. – philwinkle Mar 6 '13 at 15:23
  • You may be right about the re-instantiation; but even if it's purely academic it felt like a worthwhile question to ask. – philwinkle Mar 6 '13 at 15:24
6

You can use reset();

Mage::getModel('catalog/product')->load(1)->reset();

that calls _clearData, if you check in the Mage_Catalog_Model_Product you can see that this function resets all variables.

| improve this answer | |
  • Obviously, yes. I'm curious if there's a way to chain getting a new object at the end of loading. Otherwise, avoid reinstantiating the model again. – philwinkle Mar 5 '13 at 22:20
  • 2
    You should be able to use reset(), that calls _clearData, if you check in the Mage_Catalog_Model_Product you can see that this function resets all variables – Sander Mangel Mar 5 '13 at 22:36
  • Good question by the way, never thought about cleaning up the instance that way – Sander Mangel Mar 5 '13 at 22:44
  • You can use clone on the model before you load it if you only need empty instances. – Petar Dzhambazov Mar 6 '13 at 12:36
  • 4
    reset() only exists for catalog/product; is this question not asking for a more generic solution? – benmarks Mar 6 '13 at 13:06
1

The reset() method is available for Mage_Catalog_Model_Product but as a whole, this isn't available.

You could add a method like this:

public function reset() {
    return new self();
}

However, you cannot then call:

$modelInstance->reset();

You would have to call:

$modelInstance = $modelInstance->reset();

Because of this, in my opinion, having your own reset() method is going to lead to misuse, and/or may confuse other developers. You are better off doing something like this:

$modelInstance = new $modelInstance;
| improve this answer | |
1

Does this not work via the clearInstance function on Mage_Core_Model_Abstract. This seems to clear references and data plus it also fires an event:

Mage::dispatchEvent($this->_eventPrefix.'_clear', $this->_getEventData());

Maybe I am missing something here though.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.