我有一个自定义产品属性,实际上具有三个可能的值“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”-方法,但我需要提交固定顺序而不是使用 DESC 或 ASC。
我在 stackoverflow 和 google 上搜索了很多,但到目前为止没有答案。
我希望你能帮帮我
谢谢
[编辑回应 magalter 的回答]
不幸的是它不起作用我之前尝试过:
$this->_collection->getSelect()->order(new Zend_Db_Expr("FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')"));
结果是“处理您的请求时出错。
$this->_collection->getSelect();
返回:
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','')
我在 phpMyAdmin 中执行了这个 sql 语句,并收到以下错误消息“'order clause' 中的未知列 'season'”
如果我按“季节” DESC 订购
$this->_collection->setOrder('season', 'DESC');
它工作......并生成以下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
但这是 DESC 的顺序,而不是我想要的预定义顺序,所以我不能使用它。
我需要的是上面的 SQL 语句
ORDER BY FIELD(season, 'Frühjahr/Sommer','Herbst/Winter','')
反而
ORDER BY `season` DESC
另一个想法???