/** * Magento bug fix */ public function save(Mage_Core_Model_Abstract $object) { if ($object->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; }
/** * Overwritten save method, updates data on duplicate key * * @param Mage_Core_Model_Abstract $object Data object * @return Aoe_TranslationLogger_Model_Resource_Translation_Logging */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_serializeFields($object); $this->_beforeSave($object); $this->_getWriteAdapter()->insertIgnore($this->getMainTable(), $this->_prepareDataForSave($object)); $this->unserializeFields($object); $this->_afterSave($object); return $this; }
/** * @param Aoe_Scheduler_Model_Job $object * * @return $this */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $this->delete($object); } if (!$object instanceof Aoe_Scheduler_Model_Job) { throw new InvalidArgumentException(sprintf("Expected object of type 'Aoe_Scheduler_Model_Job' got '%s'", get_class($object))); } if (!$object->getJobCode()) { Mage::throwException('Invalid data. Must have job code.'); } $this->_serializeFields($object); $this->_beforeSave($object); $newValues = $this->getJobDataFromModel($object); $oldValues = $this->getJobDataFromDb($object->getJobCode()); $defaultValues = $this->getJobDataFromXml($object->getJobCode()); // Generate key/value lists for Update and Insert $updateValues = array_intersect_key($newValues, $oldValues); $insertValues = array_diff_key($newValues, $oldValues); // Remove Updates and Inserts that match defaults $updateValues = array_diff_assoc($updateValues, $defaultValues); $insertValues = array_diff_assoc($insertValues, $defaultValues); // Remove empty value inserts if this is a DB only job if (empty($defaultValues)) { foreach ($insertValues as $k => $v) { if ($v === '' || $v === null) { unset($insertValues[$k]); } } } // Generate key/value lists for Delete (Old values, not being updated, that are identical to default values) $deleteValues = array_intersect_assoc(array_diff_key($oldValues, $updateValues), $defaultValues); $pathPrefix = $this->getJobPathPrefix($object->getJobCode()) . '/'; $adapter = $this->_getWriteAdapter(); foreach ($updateValues as $k => $v) { $adapter->update($this->getMainTable(), array('value' => $v), array('scope = ?' => 'default', 'scope_id = ?' => 0, 'path = ?' => $pathPrefix . $k)); } foreach ($insertValues as $k => $v) { $adapter->insert($this->getMainTable(), array('scope' => 'default', 'scope_id' => 0, 'path' => $pathPrefix . $k, 'value' => $v)); } foreach ($deleteValues as $k => $v) { $adapter->delete($this->getMainTable(), array('scope = ?' => 'default', 'scope_id = ?' => 0, 'path = ?' => $pathPrefix . $k)); } if (count($updateValues) || count($insertValues) || count($deleteValues)) { Mage::getConfig()->reinit(); } $this->unserializeFields($object); $this->_afterSave($object); return $this; }
/** * Save Product Index data (forced save) * * @param Mage_Reports_Model_Product_Index_Abstract $object * @return Mage_Reports_Model_Resource_Product_Index_Abstract */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_serializeFields($object); $this->_beforeSave($object); $this->_checkUnique($object); $data = $this->_prepareDataForSave($object); unset($data[$this->getIdFieldName()]); $matchFields = array('product_id', 'store_id'); Mage::getResourceHelper('reports')->mergeVisitorProductIndex($this->getMainTable(), $data, $matchFields); $this->unserializeFields($object); $this->_afterSave($object); return $this; }
/** * Save object object data * * @param Mage_Core_Model_Abstract $object * @return Mage_Core_Model_Mysql4_Abstract */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_beforeSave($object); $this->_checkUnique($object); if (!is_null($object->getId())) { $condition = $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $object->getId()); $this->_getWriteAdapter()->update($this->getMainTable(), $this->_prepareDataForSave($object), $condition); } else { $this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object)); $object->setId($this->_getWriteAdapter()->lastInsertId($this->getMainTable())); } $this->_afterSave($object); return $this; }
/** * Save object object data * * @param Mage_Core_Model_Abstract $object * @return Mage_Core_Model_Mysql4_Abstract */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $this->delete($object); } $this->_beforeSave($object); $this->_checkUnique($object); // create an API object for communication $apiobject = $this->createObject(); if (!is_null($object->getId())) { // clone the data to the apiobject $data = $this->_prepareDataForSave($object); foreach ($data as $k => $v) { $apiobject->setField($k, $v); } // save using the API $apiobject->save(); } else { // clone the data to the apiobject $data = $this->_prepareDataForSave($object); foreach ($data as $k => $v) { $apiobject->setField($k, $v); } // insert using the API $apiobject->add(); // Clone the data from the api object to the Varien object $newfields = $apiobject->getFields(); $data = array(); foreach ($newfields as $newfield) { $data[$newfield->get(Pap_Api_Object::FIELD_NAME)] = $newfield->get(Pap_Api_Object::FIELD_VALUE); } $object->setData($data); } // TODO: What if there were errors? What do we do? // For the moment, we'll just log errors for debugging purposes. Mage::log($apiobject->getMessage()); $this->_afterSave($object); return $this; }
/** * Save object * * @param Cm_Mongo_Model_Abstract|Mage_Core_Model_Abstract $object * @throws Mage_Core_Exception * @throws MongoCursorException * @return Cm_Mongo_Model_Resource_Abstract */ public function save(Mage_Core_Model_Abstract $object) { if ($object->isDeleted()) { return $object->delete(); } $this->_beforeSave($object); $object->setLastUpdateStatus(NULL); // TRUE, do insert if ($object->isObjectNew()) { // Set created and updated timestamps $this->setTimestamps($object, $this->getEntitySchema()->created_timestamp, $this->getEntitySchema()->updated_timestamp); // Collect data for mongo $data = $this->dehydrate($object); $ops = $object->getPendingOperations(); // Set autoincrement if (empty($data['_id']) && $this->getEntitySchema()->autoincrement) { $data['_id'] = $this->getAutoIncrement(); } // Translate $set operations to simple insert data if possible if (isset($ops['$set'])) { foreach ($ops['$set'] as $key => $value) { if (strpos($key, '.') === false) { $data[$key] = $value; unset($ops['$set'][$key]); } // @TODO - expand . delimited keys } if (!count($ops['$set'])) { unset($ops['$set']); } } // Get insert options, merge default with instance-specific options $options = array('safe' => TRUE); if ($additionalSaveOptions = $object->getAdditionalSaveOptions()) { unset($additionalSaveOptions['upsert'], $additionalSaveOptions['multiple']); $options = array_merge($options, $additionalSaveOptions); } // Insert document (throws exception on failure) $this->_getWriteCollection()->insert($data, $options); if (!$object->hasData('_id') || $data['_id'] != $object->getData('_id')) { $object->setData('_id', $data['_id'])->setOrigData('_id', $data['_id']); } // Execute any pending operations if ($ops) { $object->setLastUpdateStatus($this->update($object, $ops)); } else { if ($object->getAdditionalSaveCriteria()) { $object->setAdditionalSaveCriteria(); $object->setLastUpdateStatus(TRUE); } } } else { if ($object->isObjectNew() === FALSE) { if (!$object->getId()) { throw new Mage_Core_Exception('Cannot save existing object without id.'); } // Set updated timestamp only $this->setTimestamps($object, FALSE, $this->getEntitySchema()->updated_timestamp); // Collect data for mongo and update using atomic operators $data = $this->getDataChangesForUpdate($object); $ops = $object->getPendingOperations(); if (isset($ops['$set'])) { $ops['$set'] = array_merge($data, (array) $ops['$set']); } else { if ($data) { $ops['$set'] = $data; } } // Undo unsets that are overridden by sets if (isset($ops['$unset']) && isset($ops['$set'])) { foreach ($ops['$unset'] as $key => $value) { if (isset($ops['$set'][$key])) { unset($ops['$unset'][$key]); } } if (!count($ops['$unset'])) { unset($ops['$unset']); } } if ($ops) { $object->setLastUpdateStatus($this->update($object, $ops)); } } else { // Created timestamps not available on upsert $this->setTimestamps($object, FALSE, $this->getEntitySchema()->updated_timestamp); // Collect data for upsert. If no operations then use data, otherwise add data to $set operation $data = $this->dehydrate($object); if ($ops = $object->getPendingOperations()) { if (isset($ops['$set'])) { $ops['$set'] = array_merge($data, (array) $ops['$set']); } else { $ops['$set'] = $data; } $data = $ops; } $result = $this->update($object, $data, array('upsert' => TRUE)); if ($result instanceof MongoId) { $object->setData('_id', $result); } $object->setLastUpdateStatus(!!$result); } } // Clear pending operations $object->resetPendingOperations(); $object->isObjectNew(FALSE); $this->_afterSave($object); return $this; }