/** * {@inheritDoc} * * Exceptions caught. * * @param object $object * @param string $table * @return mixed */ protected function _getLoadAttributesSelect($object, $table) { try { return parent::_getLoadAttributesSelect($object, $table); } catch (Exception $e) { Mage::logException($e); } }
/** * @magentoDataIsolation enabled * @magentoDataFixture mediaImportImageFixture */ public function testSaveMediaImage() { if (Magento_Test_Bootstrap::getInstance()->getDbVendorName() != 'mysql') { $this->markTestIncomplete('bug: MAGETWO-4227'); } $attribute = Mage::getModel('Mage_Catalog_Model_Entity_Attribute'); $attribute->loadByCode('catalog_product', 'media_gallery'); $data = implode(',', array('sku', '_attribute_set', '_type', '_product_websites', 'name', 'price', 'description', 'short_description', 'weight', 'status', 'visibility', 'tax_class_id', '_media_attribute_id', '_media_image', '_media_label', '_media_position', '_media_is_disabled')) . "\n"; $data .= implode(',', array('test_sku', 'Default', Mage_Catalog_Model_Product_Type::DEFAULT_TYPE, 'base', 'Product Name', '9.99', 'Product description', 'Short desc.', '1', Mage_Catalog_Model_Product_Status::STATUS_ENABLED, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, 0, $attribute->getId(), 'magento_image.jpg', 'Image Label', '1', '0')) . "\n"; $data = 'data://text/plain;base64,' . base64_encode($data); $fixture = new Mage_ImportExport_Model_Import_Source_Csv($data); foreach (new Mage_Catalog_Model_Resource_Product_Collection() as $product) { $this->fail("Unexpected precondition - product exists: '{$product->getId()}'."); } $this->_model->setSource($fixture)->setParameters(array('behavior' => Mage_ImportExport_Model_Import::BEHAVIOR_APPEND))->isDataValid(); $this->_model->importData(); $resource = new Mage_Catalog_Model_Resource_Product(); $productId = $resource->getIdBySku('test_sku'); // fixture $product = Mage::getModel('Mage_Catalog_Model_Product'); $product->load($productId); $gallery = $product->getMediaGalleryImages(); $this->assertInstanceOf('Varien_Data_Collection', $gallery); $items = $gallery->getItems(); $this->assertCount(1, $items); $item = array_pop($items); $this->assertInstanceOf('Varien_Object', $item); $this->assertEquals('/m/a/magento_image.jpg', $item->getFile()); $this->assertEquals('Image Label', $item->getLabel()); }
/** * Delete the document from MongoDB afer the product have been saved * * @param Mage_Catalog_Model_Product $object The deleted product * * @return Smile_MongoCore_Model_Resource_Override_Catalog_Product Self reference */ protected function _afterDelete(Varien_Object $object) { parent::_afterDelete($object); $id = null; if (is_numeric($object)) { $id = (int) $object; } elseif ($object instanceof Varien_Object) { $id = (int) $object->getId(); } if (!is_null($id)) { $removeFilter = $this->getIdsFilter($id); $collection = $this->_getDocumentCollection(); $collection->remove($removeFilter, array('justOne' => false)); } return $this; }
/** * Explicit save of an attribute : we first save the related product in Mongo to ensure the attribute * is correctly updated there as well as in MySQL db * * @param Varien_Object $object object * @param string $attributeCode attribute code * * @throws Exception * @return $this */ public function saveAttribute(Varien_Object $object, $attributeCode) { $attribute = $this->getAttribute($attributeCode); $newValue = $object->getData($attributeCode); if ($attribute->isValueEmpty($newValue)) { $newValue = null; } // Retrieve the collection to be updated // Provide a raw MongoCollection object pointing // to catalog_product_entity collection $collection = $this->_getDocumentCollection(); // We update only the document matching the currently edited product $updateCond = $this->getIdsFilter($object->getId()); // By default => attribute data sould be stored into the product current store scope $storeId = $object->getStoreId(); $updateData = array(); if (!$attribute->isScopeWebsite() && $storeId != Mage_Core_Model_App::ADMIN_STORE_ID) { if ($object->isObjectNew() === true || $attribute->isScopeGlobal()) { // If product is new we store it into the default store instead of the current one $storeId = 'attr_' . $this->getDefaultStoreId(); } else { $storeId = 'attr_' . $storeId; } if (!is_string($newValue) || $newValue != '') { // Push saved values into the saved document $fieldName = $storeId . '.' . $attribute->getAttributeCode(); $updateData[$fieldName] = $this->_prepareValueForDocumentSave($attribute, $newValue); } $collection->update($updateCond, array('$set' => $updateData), array('upsert' => true)); } else { $store = Mage::app()->getStore($storeId); $websiteStoreIds = $store->getWebsite()->getStoreIds(); foreach ($websiteStoreIds as $storeId) { // Push saved values into the saved document $attrIndex = 'attr_' . $storeId . '.' . $attributeCode; $collection->update($updateCond, array('$set' => array($attrIndex => $newValue)), array('multiple' => true)); } } $object->isObjectNew(false); if (in_array($attributeCode, $this->getSqlAttributesCodes())) { return parent::saveAttribute($object, $attributeCode); } return $this; }