/** * Retrieve Select for update Flat data * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store, $hasValueField = true) { $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $valueExpr = new Zend_Db_Expr("IFNULL(t2.value, t1.value)"); $select = $this->_getReadAdapter()->select()->joinLeft(array('t1' => $attributeTable), "`e`.`entity_id`=`t1`.`entity_id` AND `e`.`child_id`=`t1`.`entity_id`", array())->joinLeft(array('t2' => $attributeTable), "`t2`.`entity_id`=`t1`.`entity_id`" . " AND `t1`.`entity_type_id`=`t2`.`entity_type_id`" . " AND `t1`.`attribute_id`=`t2`.`attribute_id`" . " AND `t2`.`store_id`={$store}", array($attributeCode => $valueExpr)); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $select->joinLeft(array('to1' => $this->getTable('eav/attribute_option_value')), "`to1`.`option_id`={$valueExpr}" . " AND `to1`.`store_id`='0'", array())->joinLeft(array('to2' => $this->getTable('eav/attribute_option_value')), "`to2`.`option_id`={$valueExpr}" . " AND `to2`.`store_id`='{$store}'", array($attributeCode . '_value' => "IFNULL(`to2`.`value`, `to1`.`value`)")); } $select->where('t1.entity_type_id=?', $attribute->getEntityTypeId())->where('t1.attribute_id=?', $attribute->getId())->where('t1.store_id=?', 0); return $select; }
/** * Retrieve Select for update Flat data * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store, $hasValueField = true) { $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $joinConditionTemplate = "`e`.`entity_id`=`%s`.`entity_id`" . " AND `%s`.`entity_type_id` = " . $attribute->getEntityTypeId() . " AND `%s`.`attribute_id` = " . $attribute->getId() . " AND `%s`.`store_id` = %d"; $joinCondition = sprintf($joinConditionTemplate, 't1', 't1', 't1', 't1', Mage_Core_Model_App::ADMIN_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= " AND `e`.`child_id`=`t1`.`entity_id`"; } $valueExpr = new Zend_Db_Expr("IF(t2.value_id>0, t2.value, t1.value)"); $select = $this->_getReadAdapter()->select()->joinLeft(array('t1' => $attributeTable), $joinCondition, array())->joinLeft(array('t2' => $attributeTable), sprintf($joinConditionTemplate, 't2', 't2', 't2', 't2', $store), array($attributeCode => $valueExpr)); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $select->joinLeft(array('to1' => $this->getTable('eav/attribute_option_value')), "`to1`.`option_id`={$valueExpr}" . " AND `to1`.`store_id`='0'", array())->joinLeft(array('to2' => $this->getTable('eav/attribute_option_value')), "`to2`.`option_id`={$valueExpr}" . " AND `to2`.`store_id`='{$store}'", array($attributeCode . '_value' => "IFNULL(`to2`.`value`, `to1`.`value`)")); } if ($attribute->getFlatAddChildData()) { $select->where("e.is_child=?", 0); } return $select; }
/** * Insert or Update attribute data * * @param Mage_Catalog_Model_Abstract $object * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param mixed $value * @return Mage_Catalog_Model_Resource_Abstract */ protected function _saveAttributeValue($object, $attribute, $value) { $write = $this->_getWriteAdapter(); //set default store id $storeId = Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID; $table = $attribute->getBackend()->getTable(); /** * If we work in single store mode all values should be saved just * for default store id * In this case we clear all not default values */ if (Mage::app()->isSingleStoreMode()) { $storeId = $this->getDefaultStoreId(); $write->delete($table, array('attribute_id = ?' => $attribute->getAttributeId(), 'entity_id = ?' => $object->getEntityId(), 'store_id <> ?' => $storeId)); } $data = new Varien_Object(array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute))); $bind = $this->_prepareDataForTable($data, $table); if ($attribute->isScopeStore()) { /** * Update attribute value for store */ $this->_attributeValuesToSave[$table][] = $bind; } else { if ($attribute->isScopeWebsite() && $storeId != $this->getDefaultStoreId()) { /** * Update attribute value for website */ $storeIds = Mage::app()->getStore($storeId)->getWebsite()->getStoreIds(true); foreach ($storeIds as $storeId) { $bind['store_id'] = (int) $storeId; $this->_attributeValuesToSave[$table][] = $bind; } } else { /** * Update global attribute value */ $bind['store_id'] = $this->getDefaultStoreId(); $this->_attributeValuesToSave[$table][] = $bind; } } return $this; }
/** * Retrieve Select for update Flat data * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store, $hasValueField = true) { $adapter = $this->_getReadAdapter(); $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $joinConditionTemplate = "%s.entity_id = %s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', Mage_Core_Model_App::ADMIN_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select Varien_Db_Select */ $select = $adapter->select()->joinLeft(array('t1' => $attributeTable), $joinCondition, array())->joinLeft(array('t2' => $attributeTable), sprintf($joinConditionTemplate, 'e', 't2', 't2', 't2', 't2', $store), array($attributeCode => $valueExpr)); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value'); $select->joinLeft(array('to1' => $this->getTable('eav/attribute_option_value')), "to1.option_id = {$valueExpr} AND to1.store_id = 0", array())->joinLeft(array('to2' => $this->getTable('eav/attribute_option_value')), $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store), array($attributeCode . '_value' => $valueIdExpr)); } if ($attribute->getFlatAddChildData()) { $select->where("e.is_child = ?", 0); } return $select; }
/** * Insert entity attribute value * * @param Varien_Object $object * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param mixed $value * @return Mage_Catalog_Model_Resource_Abstract */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) Mage::app()->getStore($object->getStoreId())->getId(); if ($attribute->getIsRequired() && $this->getDefaultStoreId() != $storeId) { $table = $attribute->getBackend()->getTable(); $select = $this->_getReadAdapter()->select()->from($table)->where('entity_type_id = ?', $attribute->getEntityTypeId())->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where('entity_id = ?', $object->getEntityId()); $row = $this->_getReadAdapter()->fetchOne($select); if (!$row) { $data = new Varien_Object(array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute))); $bind = $this->_prepareDataForTable($data, $table); $this->_getWriteAdapter()->insertOnDuplicate($table, $bind, array('value')); } } return $this->_saveAttributeValue($object, $attribute, $value); }
/** * Retrieve Select For Flat Attribute update * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $storeId * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $storeId) { $adapter = $this->_getReadAdapter(); $joinConditionTemplate = "%s.entity_id=%s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', Mage_Core_Model_App::ADMIN_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select Varien_Db_Select */ $select = $adapter->select()->joinLeft(array('t1' => $attribute->getBackend()->getTable()), $joinCondition, array())->joinLeft(array('t2' => $attribute->getBackend()->getTable()), sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $storeId), array($attribute->getAttributeCode() => $valueExpr)); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child = ?", 0); } return $select; }
/** * Insert entity attribute value * * @param Varien_Object $object * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param mixed $value * @return Mage_Eav_Model_Entity_Abstract */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = Mage::app()->getStore($object->getStoreId())->getId(); if ($attribute->getIsRequired() && $this->getDefaultStoreId() != $storeId) { $bind = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute)); $this->_getWriteAdapter()->insertOnDuplicate($attribute->getBackend()->getTable(), $bind, array('value')); } return $this->_saveAttributeValue($object, $attribute, $value); // $entityIdField = $attribute->getBackend()->getEntityIdField(); // $row = array( // $entityIdField => $object->getId(), // 'entity_type_id'=> $object->getEntityTypeId(), // 'attribute_id' => $attribute->getId(), // 'value' => $this->_prepareValueForSave($value, $attribute), // 'store_id' => $this->getDefaultStoreId() // ); // // $fields = array(); // $bind = array(); // foreach ($row as $k => $v) { // $fields[] = $this->_getWriteAdapter()->quoteIdentifier($k); // $bind[':' . $k] = $v; // } // // $sql = sprintf('INSERT IGNORE INTO %s (%s) VALUES(%s)', // $this->_getWriteAdapter()->quoteIdentifier($attribute->getBackend()->getTable()), // implode(',', $fields), // implode(',', array_keys($bind))); // // $this->_getWriteAdapter()->query($sql, $bind); // if (!$lastId = $this->_getWriteAdapter()->lastInsertId()) { // $select = $this->_getReadAdapter()->select() // ->from($attribute->getBackend()->getTable(), 'value_id') // ->where($entityIdField . '=?', $row[$entityIdField]) // ->where('entity_type_id=?', $row['entity_type_id']) // ->where('attribute_id=?', $row['attribute_id']) // ->where('store_id=?', $row['store_id']); // $lastId = $select->query()->fetchColumn(); // } // if ($object->getStoreId() != $this->getDefaultStoreId()) { // $this->_updateAttribute($object, $attribute, $lastId, $value); // } // return $this; }
/** * Retrieve Select For Flat Attribute update * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store) { $joinCondition = "`e`.`entity_id`=`t1`.`entity_id`"; if ($attribute->getFlatAddChildData()) { $joinCondition .= " AND `e`.`child_id`=`t1`.`entity_id`"; } $select = $this->_getReadAdapter()->select()->joinLeft(array('t1' => $attribute->getBackend()->getTable()), $joinCondition, array())->joinLeft(array('t2' => $attribute->getBackend()->getTable()), "t2.entity_id = t1.entity_id" . " AND t1.entity_type_id = t2.entity_type_id" . " AND t1.attribute_id = t2.attribute_id" . " AND t2.store_id = {$store}", array($attribute->getAttributeCode() => "IF(t2.value_id>0, t2.value, t1.value)"))->where("t1.entity_type_id=?", $attribute->getEntityTypeId())->where("t1.attribute_id=?", $attribute->getId())->where("t1.store_id=?", 0); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child=?", 0); } return $select; }
/** * Retrieve Select For Flat Attribute update * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store) { return $this->_getReadAdapter()->select()->joinLeft(array('t1' => $attribute->getBackend()->getTable()), "`e`.`entity_id`=`t1`.`entity_id` AND `e`.`child_id`=`t1`.`entity_id`", array())->joinLeft(array('t2' => $attribute->getBackend()->getTable()), "t2.entity_id = t1.entity_id" . " AND t1.entity_type_id = t2.entity_type_id" . " AND t1.attribute_id = t2.attribute_id" . " AND t2.store_id = {$store}", array($attribute->getAttributeCode() => "IFNULL(t2.value, t1.value)"))->where("t1.entity_type_id=?", $attribute->getEntityTypeId())->where("t1.attribute_id=?", $attribute->getId())->where("t1.store_id=?", 0)->where("e.is_child=?", 0); }
/** * Retrieve Select For Flat Attribute update * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param int $store * @return Varien_Db_Select */ public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store) { $joinConditionTemplate = "`e`.`entity_id`=`%s`.`entity_id`" . " AND `%s`.`entity_type_id` = " . $attribute->getEntityTypeId() . " AND `%s`.`attribute_id` = " . $attribute->getId() . " AND `%s`.`store_id` = %d"; $joinCondition = sprintf($joinConditionTemplate, 't1', 't1', 't1', 't1', Mage_Core_Model_App::ADMIN_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= " AND `e`.`child_id`=`t1`.`entity_id`"; } $select = $this->_getReadAdapter()->select()->joinLeft(array('t1' => $attribute->getBackend()->getTable()), $joinCondition, array())->joinLeft(array('t2' => $attribute->getBackend()->getTable()), sprintf($joinConditionTemplate, 't2', 't2', 't2', 't2', $store), array($attribute->getAttributeCode() => "IF(t2.value_id>0, t2.value, t1.value)")); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child=?", 0); } return $select; }
/** * Return increment needed for SKU uniqueness * * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute * @param Mage_Catalog_Model_Product $object * @return int */ protected function _getLastSimilarAttributeValueIncrement($attribute, $object) { $adapter = $this->getAttribute()->getEntity()->getReadConnection(); $select = $adapter->select(); $value = $object->getData($attribute->getAttributeCode()); $bind = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_code' => trim($value) . '-%'); $select->from($this->getTable(), $attribute->getAttributeCode())->where('entity_type_id = :entity_type_id')->where($attribute->getAttributeCode() . ' LIKE :attribute_code')->order(array('entity_id DESC', $attribute->getAttributeCode() . ' ASC'))->limit(1); $data = $adapter->fetchOne($select, $bind); return abs((int) str_replace($value, '', $data)); }