Magento: Check if object is new after save – isObjectNew
21 Jan 2012
In Magento If you have case when you need to check if some object is new ( just created ) in after save callback, in some function where you use $object->save() or somewhere else, you can use next method:
$object->isObjectNew();
This function is available in all Magento CE releases >= 1.4.0.0
Some examples:
In observer:
public function myAfterSaveHandler(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getDataObject();
if($object->isObjectNew()) {
//do something
}
}
In save action of some controller:
public function saveAction() {
...
$object->save();
...
if($object->isObjectNew()) {
//do something
}
...
}
How it works:
In Mage_Core_Model_Abstract we have flag:
/**
* Flag which allow detect object state: is it new object (without id) or existing one (with id)
*
* @var bool
*/
protected $_isObjectNew = null;
and next function, that checks the state of the flag:
/**
* Check object state (true - if it is object without id on object just created)
* This method can help detect if object just created in _afterSave method
* problem is what in after save object has id and we can't detect what object was
* created in this transaction
*
* @param bool $flag
* @return bool
*/
public function isObjectNew($flag=null)
{
if ($flag !== null) {
$this->_isObjectNew = $flag;
}
if ($this->_isObjectNew !== null) {
return $this->_isObjectNew;
}
return !(bool)$this->getId();
}
The key moment here is in before save callback:
/**
* Processing object before save data
*
* @return Mage_Core_Model_Abstract
*/
protected function _beforeSave()
{
if (!$this->getId()) {
$this->isObjectNew(true);
}
Mage::dispatchEvent('model_save_before', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_save_before', $this->_getEventData());
return $this;
}
As you can see, in _beforeSave(), if the object hasn’t id we set the flag $_isObjectNew to TRUE. My first thought when I tried to check if the object is new was to try to inspect object’s data and origData … values like $object->getData(‘id’) and $object->getOrigData(‘id’)
I suspected, that will have something like empty origData()
… YES …, but NO
Some thoughts:
If you read with attention, may be noticed, that the function isObjectNew is public and it allows you to assign value to the flag $_ isObjectNew. We need to pay attention in next situations:
1 ) We set isObjectNew to true in before save handler:
public function myBeforeSaveHandler(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getDataObject();
$object->isObjectNew(true);
}
public function myAfterSaveHandler(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getDataObject();
if($object->isObjectNew()) {
//$object->isObjectNew() will return true
//do something
}
}
2 ) We set isObjectNew to false in before save handler:
public function myBeforeSaveHandler(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getDataObject();
$object->isObjectNew(false);
}
public function myAfterSaveHandler(Varien_Event_Observer $observer) {
$object = $observer->getEvent()->getDataObject();
if(!$object->isObjectNew()) {
//$object->isObjectNew() will return false
//do something
}
}
It’s good to keep in mind, that we can set isObjectNew(false) in some before save handler, but it depends of the logic of your custom extension.
So, that’s my thought about isObjectNew function. Do you have any?
Will be happy if this article will help to somebody!
Tags: isObjectNew, magento
This entry was posted on Saturday, January 21st, 2012 at 2:56 pm and is filed under Magento. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
-
Anonymous
-
http://twitter.com/tegansnyder Tegan Snyder
-
ceckoslab

