Magento で「持つ」SQL 関数が必要です。私の知る限り、having 関数はありません。
だから私はコレクションクラスでそれを実装しようとします
public function addAttributeHaving($attribute)
{
$this->getSelect()->having($attribute);
return $this;
}
そして、この機能を使用すると
$collectionHotel->addAttributeToSort($order, $dir)
->addAttributeHaving('MIN(`apptha_booking_room_types`.`room_price_per_night`) >= '.$lowestprice)
->addAttributeHaving('MIN(`apptha_booking_room_types`.`room_price_per_night`) <= '.$highestprice);
出力クエリは問題ありません:
SELECT
`e` . *,
MIN(`apptha_booking_room_types`.room_price_per_night) AS `lowestprice`,
IF(at_status.value_id > 0,
at_status.value,
at_status_default.value) AS `status`,
IF(at_apptha_hotel_period_to.value_id > 0,
at_apptha_hotel_period_to.value,
at_apptha_hotel_period_to_default.value) AS `apptha_hotel_period_to`,
`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`
LEFT JOIN
`apptha_booking_room_types` ON (apptha_booking_room_types.entity_id = e.entity_id)
INNER JOIN
`catalog_product_entity_int` AS `at_status_default` ON (`at_status_default`.`entity_id` = `e`.`entity_id`)
AND (`at_status_default`.`attribute_id` = '89')
AND `at_status_default`.`store_id` = 0
LEFT JOIN
`catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = `e`.`entity_id`)
AND (`at_status`.`attribute_id` = '89')
AND (`at_status`.`store_id` = 1)
INNER JOIN
`catalog_product_entity_datetime` AS `at_apptha_hotel_period_to_default` ON (`at_apptha_hotel_period_to_default`.`entity_id` = `e`.`entity_id`)
AND (`at_apptha_hotel_period_to_default`.`attribute_id` = '168')
AND `at_apptha_hotel_period_to_default`.`store_id` = 0
LEFT JOIN
`catalog_product_entity_datetime` AS `at_apptha_hotel_period_to` ON (`at_apptha_hotel_period_to`.`entity_id` = `e`.`entity_id`)
AND (`at_apptha_hotel_period_to`.`attribute_id` = '168')
AND (`at_apptha_hotel_period_to`.`store_id` = 1)
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
WHERE
(`e`.`type_id` = 'hotel')
AND (IF(at_status.value_id > 0,
at_status.value,
at_status_default.value) = '1')
AND (IF(at_apptha_hotel_period_to.value_id > 0,
at_apptha_hotel_period_to.value,
at_apptha_hotel_period_to_default.value) >= '2012-11-09')
GROUP BY `e`.`entity_id`
HAVING (MIN(`apptha_booking_room_types`.`room_price_per_night`) >= 0)
AND (MIN(`apptha_booking_room_types`.`room_price_per_night`) <= 999999999)
ORDER BY `lowestprice` asc
このクエリを MySQL で実行すると、出力は問題ありません。
しかし、私が使用しようとすると
$collectionHotel->load();
このエラーが表示されます:
SQLSTATE[42S22]: 列が見つかりません: 1054 不明な列 'apptha_booking_room_types.room_price_per_night' in 'having clause'</p>
できることはすべて試しましたが、まだこの問題を解決できません。誰でも助けることができますか?
あるいは、having() 関数を実装するより良い方法はありますか?