isDeleted()) { return $this->delete($object); } $this->_beforeSave($object); $this->_checkUnique($object); if (!is_null($object->getId())) { $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $object->getId()); /** * Not auto increment primary key support */ if ($this->_isPkAutoIncrement) { $this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition); } else { $select = $this->_getWriteAdapter()->select()->from($this->getMainTable(), array($this->getIdFieldName())) ->where($condition); if ($this->_getWriteAdapter()->fetchOne($select) !== false) { $this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition); } else { $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object)); } } } else { $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object)); $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable())); } $this->_afterSave($object); return $this; } protected function _prepareDataForSave(Mage_Core_Model_Abstract $object) { if ($this->_isPkAutoIncrement && !$object->getId()) { $object->setCreatedAt(now()); } else { $object->setCreatedAt(now()); } $object->setUpdatedAt(now()); $data = parent::_prepareDataForSave($object); return $data; } }