Query Magento database object oriented
Naturally you can query Magento database directly as described in my earlier post about querying Magento database. But a much smarter way is it, to use exiting Magento objects and constructs like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php /** * Sets position (sort order) values for the given attribute for a given option value. Therefore the given store specific value is * used to lookup the necessary option id to identify the correct option * @param sting $sAttributeCode Magento attribute code for that option value position should be set * @param sting $sValue option value in the given store * @param int $iPosition position (sort order) to set for given option value * @param int $iReferenceStoreId store to search for a option label * @throws Zend_Db_Adapter_Exception */ public function setAttributeOptionSorting($sAttributeCode, $sValue, $iPosition, $iReferenceStoreId) { /* @var Varien_Db_Adapter_Pdo_Mysql $oWriteConnection */ $oWriteConnection = Mage::getSingleton('core/resource')->getConnection('core_write'); $sOptionTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option'); $sOptionLableTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option_value'); $iAttributeId = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $sAttributeCode)->getId(); $oSelect = new Varien_Db_Select($oWriteConnection); $oSelect->from( ['eao' => $sOptionTable], ['option_id'] )->join( ['eaov' => $sOptionLableTable], "eao.option_id = eaov.option_id AND eaov.store_id = " . $iReferenceStoreId, '' )->where( 'eao.attribute_id = ?', $iAttributeId )->where( 'eaov.value = ?', $sValue ); $aResult = $oWriteConnection->fetchCol($oSelect); foreach ($aResult as $iOptionId) { $oWriteConnection->update( $sOptionTable, ['sort_order' => $iPosition], $oWriteConnection->quoteInto('option_id = ?', $iOptionId) ); } } |
This is a simplified version of a function (better to say shell script class) that sets attribute value positions for a certain […]