如何使用ORDER BY FIELD对magento产品集合进行排序

Man*_*uel 4 magento

我有一个自定义产品属性,实际上有三个可能的值"Frühjahr/ Sommer","Herbst/Winter",""(空).

我希望按照预定的顺序按此值对产品集合进行排序,例如"Frühjahr/ Sommer","Herbst/Winter",""或"Herbst/Winter","Frühjahr/ Sommer",""

这是交替的.空值应该在结尾处,但可能还有更多值,因此它必须是固定的预定义顺序.

我需要执行以下MySQL ORDER BY FIELD(saison,"Frühjahr/ Sommer","Herbst/Winter","")

问题是我不知道如何在Magento中执行此命令.我知道" - > setOrder"-Method但我需要提交一个固定的顺序而不是使用DESC或ASC.

我在stackoverflow和google上搜索了很多但到目前为止还没有答案.

我希望你能帮帮我

谢谢

[编辑回应magalter的答案]

不幸的是它不起作用我之前尝试过:

$this->_collection->getSelect()->order(new Zend_Db_Expr("FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')"));
Run Code Online (Sandbox Code Playgroud)

结果是"处理您的请求时出错.

$this->_collection->getSelect();
Run Code Online (Sandbox Code Playgroud)

返回:

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='124' INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 ORDER BY FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')
Run Code Online (Sandbox Code Playgroud)

我在phpMyAdmin中执行了这个sql语句,并在'order clause'中收到以下错误消息"Unknown column' season'"

如果我按"季节"DESC订购

$this->_collection->setOrder('season', 'DESC');
Run Code Online (Sandbox Code Playgroud)

它工作...并生成以下sql语句

SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, IF(season_option_value_t2.value_id IS NULL, season_option_value_t1.value, season_option_value_t2.value) AS `season` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id='124' INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 LEFT JOIN `catalog_product_entity_int` AS `season_t1` ON e.entity_id=season_t1.entity_id AND season_t1.attribute_id='180' AND season_t1.store_id=0 LEFT JOIN `catalog_product_entity_int` AS `season_t2` ON e.entity_id=season_t2.entity_id AND season_t2.attribute_id='180' AND season_t2.store_id='1' LEFT JOIN `eav_attribute_option_value` AS `season_option_value_t1` ON season_option_value_t1.option_id=IF(season_t2.value_id > 0, season_t2.value, season_t1.value) AND season_option_value_t1.store_id=0 LEFT JOIN `eav_attribute_option_value` AS `season_option_value_t2` ON season_option_value_t2.option_id=IF(season_t2.value_id > 0, season_t2.value, season_t1.value) AND season_option_value_t2.store_id=1 ORDER BY `season` DESC
Run Code Online (Sandbox Code Playgroud)

但这是DESC的顺序,而不是像我想要的预定义顺序,所以我不能使用它.

我需要的是上面的SQL语句

ORDER BY FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')
Run Code Online (Sandbox Code Playgroud)

代替

ORDER BY `season` DESC
Run Code Online (Sandbox Code Playgroud)

另一个想法???

fre*_*nto 6

尝试使用Zend_DB_Select顺序而不是Magento setOrder方法

$colection->getSelect()->order(..)
Run Code Online (Sandbox Code Playgroud)

例:

->order(array('line_items_per_product DESC',
                       'product_id'));
Run Code Online (Sandbox Code Playgroud)

有关 更多示例,请参见 http://framework.zend.com/manual/1.12/ru/zend.db.select.html#zend.db.select.building.order


Man*_*uel 3

最后我找到了一个解决方案。\n也许另一个用户需要它,所以这里是:

\n\n

(可能不是最好的解决方案,但它有效)

\n\n

我创建了一个新模块并扩展了 Mage_Catalog_Block_Product_List_Toolbar。\n然后我重写了“setCollection”方法,如下所示

\n\n
public function setCollection($collection)\n{\n    $this->_collection = $collection;\n\n    $this->_collection->setCurPage($this->getCurrentPage());\n\n    // we need to set pagination only if passed value integer and more that 0\n    $limit = (int)$this->getLimit();\n    if ($limit) {\n        $this->_collection->setPageSize($limit);\n    }\n    if ($this->getCurrentOrder()) {\n        $mySource = $this->_collection->getEntity()->getAttribute(\'season\');\n\n        $mySource = $this->_collection->getEntity()->getAttribute(\'season\')->getSource(); //Mage_Eav_Model_Entity_Attribute_Source_Table\n        $collection = $this->_collection;\n        $valueTable1    = \'season_t1\';\n        $valueTable2    = \'season_t2\';\n\n        $collection->getSelect()\n            ->joinLeft(\n                array($valueTable1 => $mySource->getAttribute()->getBackend()->getTable()),\n                "e.entity_id={$valueTable1}.entity_id"\n                . " AND {$valueTable1}.attribute_id=\'{$mySource->getAttribute()->getId()}\'"\n                . " AND {$valueTable1}.store_id=0",\n                array()\n            )\n            ->joinLeft(\n                array($valueTable2 => $mySource->getAttribute()->getBackend()->getTable()),\n                "e.entity_id={$valueTable2}.entity_id"\n                . " AND {$valueTable2}.attribute_id=\'{$mySource->getAttribute()->getId()}\'"\n                . " AND {$valueTable2}.store_id=\'{$collection->getStoreId()}\'",\n                array()\n            );\n        $valueExpr = $collection->getSelect()->getAdapter()\n        ->getCheckSql("{$valueTable2}.value_id > 0", "{$valueTable2}.value", "{$valueTable1}.value");\n\n        Mage::getResourceModel(\'eav/entity_attribute_option\')\n            ->addOptionValueToCollection($collection, $mySource->getAttribute(), $valueExpr);\n\n        $sortBySeason = "FIELD(IF(season_option_value_t2.value_id IS NULL, season_option_value_t1.value, season_option_value_t2.value), ";\n\n\n        $getSelectedSeason = Mage::getStoreConfig(\'sortproductsbyseason_options/general/current_season\');\n\n        if($getSelectedSeason == "herbst_winter"){\n            $sortBySeason .= "\'Fr\xc3\xbchjahr/Sommer\', \'Herbst/Winter\' ,\'\'";\n        }else{\n            $sortBySeason .= "\'Herbst/Winter\', \'Fr\xc3\xbchjahr/Sommer\' ,\'\'";\n        }\n        $sortBySeason .= ") DESC";\n\n        $this->_collection->getSelect()->order(new Zend_Db_Expr($sortBySeason));\n\n        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());\n    }\n\n    return $this;\n}   \n
Run Code Online (Sandbox Code Playgroud)\n