private function saveAttribute() { if ($this->attributeObj->getId()) { return array('result' => true, 'obj' => $this->attributeObj); } if (!$this->validate()) { return array('result' => false, 'error' => 'Attribute builder. Validation failed.'); } $this->attributeObj = Mage::getModel('catalog/resource_eav_attribute'); $data = $this->params; $data['attribute_code'] = $this->code; $data['frontend_label'] = array(Mage_Core_Model_App::ADMIN_STORE_ID => $this->primaryLabel); $data['frontend_input'] = $this->inputType; $data['entity_type_id'] = $this->entityTypeId; $data['is_user_defined'] = 1; $data['source_model'] = Mage::helper('catalog/product')->getAttributeSourceModelByInputType($this->inputType); $data['backend_model'] = Mage::helper('catalog/product')->getAttributeBackendModelByInputType($this->inputType); $data['backend_type'] = $this->attributeObj->getBackendTypeByInput($this->inputType); !isset($data['is_global']) && ($data['is_global'] = self::SCOPE_STORE); !isset($data['is_configurable']) && ($data['is_configurable'] = 0); !isset($data['is_filterable']) && ($data['is_filterable'] = 0); !isset($data['is_filterable_in_search']) && ($data['is_filterable_in_search'] = 0); !isset($data['apply_to']) && ($data['apply_to'] = array()); $this->prepareOptions($data); $this->prepareDefault($data); $this->attributeObj->addData($data); try { $this->attributeObj->save(); } catch (Exception $e) { return array('result' => false, 'error' => $e->getMessage()); } return array('result' => true, 'obj' => $this->attributeObj); }
/** * Copy all eav attribute values for a specified attribute from one table to another eav attribute table, * updating the attribute id. The old records remain in the old value table. * * @param Mage_Eav_Model_Entity_Attribute $oldAttribute * @param Mage_Eav_Model_Entity_Attribute $newAttribute * @return array Affected entity ids */ public function copyAttributeValues(Mage_Eav_Model_Entity_Attribute $oldAttribute, Mage_Eav_Model_Entity_Attribute $newAttribute) { $select = $this->_getReadAdapter()->select()->reset()->distinct(true)->from($oldAttribute->getBackendTable(), 'entity_id')->where('attribute_id=:attribute_id'); $entityIds = $this->_getReadAdapter()->fetchCol($select, array('attribute_id' => $oldAttribute->getId())); // Remove new attribute value records for entities that are being migrated $this->_getWriteAdapter()->delete($newAttribute->getBackendTable(), array('attribute_id=?' => $newAttribute->getId(), 'entity_id IN(?)' => $entityIds)); // Copy old attribute values to the new attribute $selectFields = array('entity_type_id', new Zend_Db_Expr($newAttribute->getId()), 'store_id', 'entity_id', 'value'); $insertFields = array('entity_type_id', 'attribute_id', 'store_id', 'entity_id', 'value'); $select->reset()->from($oldAttribute->getBackendTable(), $selectFields)->where('attribute_id=?', $oldAttribute->getId()); $update = $this->_getWriteAdapter()->insertFromSelect($select, $newAttribute->getBackendTable(), $insertFields, Varien_Db_Adapter_Interface::INSERT_IGNORE); $this->_getWriteAdapter()->query($update); return $entityIds; }
private function saveAttribute() { if ($this->attributeObj->getId()) { return array('result' => true, 'obj' => $this->attributeObj, 'code' => $this->attributeObj->getAttributeCode()); } if (!$this->validate()) { return array('result' => false, 'error' => 'Attribute builder. Validation failed.'); } $data = $this->params; $data['attribute_code'] = $this->code; $data['frontend_label'] = array(Mage_Core_Model_App::ADMIN_STORE_ID => $this->primaryLabel); $data['frontend_input'] = $this->inputType; $data['source_model'] = Mage::helper('catalog/product')->getAttributeSourceModelByInputType($this->inputType); $data['backend_model'] = Mage::helper('catalog/product')->getAttributeBackendModelByInputType($this->inputType); !isset($data['is_global']) && ($data['is_global'] = self::SCOPE_STORE); !isset($data['is_configurable']) && ($data['is_configurable'] = 0); !isset($data['is_filterable']) && ($data['is_filterable'] = 0); !isset($data['is_filterable_in_search']) && ($data['is_filterable_in_search'] = 0); $this->attributeObj = Mage::getModel('catalog/resource_eav_attribute'); if (is_null($this->attributeObj->getIsUserDefined()) || $this->attributeObj->getIsUserDefined() != 0) { $data['backend_type'] = $this->attributeObj->getBackendTypeByInput($this->inputType); } // default value if (empty($data['default_value'])) { unset($data['default_value']); } // --------------------------------------- !isset($data['apply_to']) && ($data['apply_to'] = array()); // prepare options foreach ($this->options as $optionValue) { $code = 'option_' . substr(sha1($optionValue), 0, 6); $data['option']['value'][$code] = array(Mage_Core_Model_App::ADMIN_STORE_ID => $optionValue); } // --------------------------------------- $this->attributeObj->addData($data); $this->attributeObj->setEntityTypeId($this->entityTypeId); $this->attributeObj->setIsUserDefined(1); try { $this->attributeObj->save(); } catch (Exception $e) { return array('result' => false, 'error' => $e->getMessage()); } return array('result' => true, 'obj' => $this->attributeObj, 'code' => $this->attributeObj->getAttributeCode()); }
/** * Gets attribute options from database * * @param \Mage_Eav_Model_Entity_Attribute $attribute * * @return array */ protected function getOptions($attribute) { $select = $this->readConnection->select()->from(array('o' => \Mage::getSingleton('core/resource')->getTableName('eav_attribute_option')))->join(array('ov' => \Mage::getSingleton('core/resource')->getTableName('eav_attribute_option_value')), 'o.option_id = ov.option_id')->where('o.attribute_id = ?', $attribute->getId())->where('ov.store_id = 0')->order('ov.option_id'); $query = $select->query(); $values = array(); foreach ($query->fetchAll() as $row) { $values[] = $row['value']; } return array('values' => $values); }
/** * Delete product data * * @param Mage_Catalog_Model_Product $product * @param Mage_Eav_Model_Entity_Attribute $attribute * @return Enterprise_GiftCard_Model_Resource_Attribute_Backend_Giftcard_Amount */ public function deleteProductData($product, $attribute) { $condition = array(); if (!$attribute->isScopeGlobal()) { if ($storeId = $product->getStoreId()) { $condition['website_id IN (?)'] = array(0, Mage::app()->getStore($storeId)->getWebsiteId()); } } $condition['entity_id=?'] = $product->getId(); $condition['attribute_id=?'] = $attribute->getId(); $this->_getWriteAdapter()->delete($this->getMainTable(), $condition); return $this; }
/** * Return entities where attribute value is * * @param array|int $entityIdsFilter * @param Mage_Eav_Model_Entity_Attribute $attribute * @param mixed $expectedValue * @return array */ public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue) { $bind = array('attribute_id' => $attribute->getId(), 'value' => $expectedValue); $select = $this->_getReadAdapter()->select()->from($attribute->getBackend()->getTable(), array('entity_id'))->where('attribute_id = :attribute_id')->where('value = :value')->where('entity_id IN(?)', $entityIdsFilter); return $this->_getReadAdapter()->fetchCol($select, $bind); }
/** * Assigns the attribute to the attribute set * * @param Mage_Eav_Model_Entity_Attribute $attribute * @param Mage_Eav_Model_Entity_Attribute_Set $attributeSet * @param int $attributeGroupId */ protected function _assignAttributeToAttributeSet($attribute, $attributeSet, $attributeGroupId) { $entityTypeId = $this->_getEntityTypeId(); $setup = Mage::getModel('eav/entity_setup', 'core_setup'); $setup->addAttributeToSet($entityTypeId, $attributeSet->getId(), $attributeGroupId, $attribute->getId()); }
private function isAttributeInAttributeSet(Mage_Eav_Model_Entity_Attribute $attribute, $attributeSetId) { $attributesMatchingInNewAttributeSet = $attribute->getResourceCollection()->setAttributeSetFilter($attributeSetId)->addFieldToFilter('entity_attribute.attribute_id', $attribute->getId())->load(); return count($attributesMatchingInNewAttributeSet) === 0; }
/** * Returns attribute meta info for record, * e.g. entity_type_id, attribute_id, etc * * @param array $row * @param Mage_Eav_Model_Entity_Attribute $attribute * @return array */ protected function _getAttributeValueInfo($row, $attribute) { return array('attribute_id' => $attribute->getId(), 'entity_type_id' => $attribute->getEntityTypeId(), $this->_getEntityIdField($attribute) => $row[$this->_getEntityIdField($attribute)]); }
private function checkIsAlreadyInSet() { /* @var $collection Mage_Eav_Model_Resource_Entity_Attribute_Collection */ $collection = Mage::getModel('eav/entity_attribute')->getResourceCollection()->setAttributeSetFilter($this->setId)->addFieldToFilter('entity_attribute.attribute_id', $this->attributeObj->getId()); return $collection->getSize() > 0; }
/** * Migrate Entries from source to target tables (if possible) * * TODO: Delete existing Select/Multiselect Values if the new Backend Type is not one of Select/Multiselect * * @param Mage_Eav_Model_Entity_Attribute $attribute Attribute Model * @param string $targetType Target Backend Type * @return void */ protected function _migrateData($attribute, $targetType) { /** @var Varien_Db_Adapter_Interface $_dbConnection */ $_dbConnection = Mage::getSingleton('core/resource')->getConnection('core_write'); // e.g. Entity is 'catalog_product' $entityTypeCode = $attribute->getEntity()->getData('entity_type_code'); // Set Backend Types for later reference $sourceType = $attribute->getBackendType(); // Create complete Entity Table names, e.g. 'catalog_product_entity_text' $sourceTable = implode([$entityTypeCode, 'entity', $sourceType], '_'); $targetTable = implode([$entityTypeCode, 'entity', $targetType], '_'); // Select all existing entries for given Attribute $srcSql = 'SELECT' . ' * FROM ' . $sourceTable . ' WHERE attribute_id = ? AND entity_type_id = ?'; /** @var Zend_Db_Statement_Interface $sourceQuery */ $sourceQuery = $_dbConnection->query($srcSql, [$attribute->getId(), $attribute->getEntity()->getData('entity_type_id')]); $this->_migrateNonSelect($targetType, $sourceQuery, $sourceType, $targetTable, $_dbConnection, $sourceTable); }
/** * Get Human Readable label for attribute value option * * @param Mage_Eav_Model_Entity_Attribute $attribute * @param int|string $attributeValueId * * @return string|boolean */ public function getAttributeAdminLabel($attribute, $attributeValueId) { $_collection = Mage::getResourceModel('eav/entity_attribute_option_collection')->setStoreFilter(0)->setAttributeFilter($attribute->getId())->load(); foreach ($_collection->toOptionArray() as $_cur_option) { if ($_cur_option['value'] == $attributeValueId) { return $_cur_option['label']; } } return false; }
/** * get the attribute set collection that an attribute belongs to * @param Mage_Eav_Model_Entity_Attribute $attribute * @return Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection */ public function getAttributeInSets($attribute) { $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->addFieldToFilter('main_table.entity_type_id', $attribute->getEntityTypeId()); $collection->getSelect()->join(array('entity_attribute' => $this->getTable('eav/entity_attribute')), 'main_table.attribute_set_id = entity_attribute.attribute_set_id', array('entity_attribute_id'))->where('entity_attribute.attribute_id = ?', $attribute->getId()); return $collection; }