Example #1
0
 /**
  * Prepare entity object data for save
  *
  * result array structure:
  * array (
  *  'newObject', 'entityRow', 'insert', 'update', 'delete'
  * )
  *
  * @param   Varien_Object $newObject
  * @return  array
  */
 protected function _collectSaveData($newObject)
 {
     $newData = $newObject->getData();
     $entityId = $newObject->getData($this->getEntityIdField());
     // define result data
     $entityRow = array();
     $insert = array();
     $update = array();
     $delete = array();
     if (!empty($entityId)) {
         $origData = $newObject->getOrigData();
         /**
          * get current data in db for this entity if original data is empty
          */
         if (empty($origData) && $newObject->isNewObject() === FALSE) {
             $origData = $this->_getOrigObject($newObject)->getOrigData();
         }
         /**
          * drop attributes that are unknown in new data
          * not needed after introduction of partial entity loading
          */
         if ($origData) {
             foreach ($origData as $k => $v) {
                 if (!array_key_exists($k, $newData)) {
                     unset($origData[$k]);
                 }
             }
         }
     } else {
         $origData = array();
     }
     $attributeCodes = array_keys($this->_attributesByCode);
     foreach ($newData as $k => $v) {
         /**
          * Check attribute information
          */
         if (is_numeric($k) || is_array($v)) {
             continue;
         }
         /**
          * Check if data key is presented in static fields or attribute codes
          */
         if (!in_array($k, $attributeCodes)) {
             continue;
         }
         $attribute = $this->getAttribute($k);
         if (empty($attribute)) {
             continue;
         }
         $attrCode = $attribute->getAttributeCode();
         /**
          * Check comparability for attribute value
          */
         if (array_key_exists($k, $origData)) {
             if ($this->_isAttributeValueEmpty($attribute, $v)) {
                 $delete[$attrCode] = 1;
             } else {
                 if ($v !== $origData[$k]) {
                     $update[$attrCode] = $v;
                 }
             }
         } else {
             if (!$this->_isAttributeValueEmpty($attribute, $v)) {
                 $insert[$attrCode] = $v;
             }
         }
     }
     $result = compact('newObject', 'insert', 'update', 'delete');
     return $result;
 }