I need a SQL function "having" in Magento. As far as I know, there is no having function.
So I try to implement it in collection class
public function addAttributeHaving($attribute)
{
$this->getSelect()->having($attribute);
return $this;
}
and when I use this function
$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);
output query is fine:
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
When I execute this query in MySQL, the output is fine.
But when I try to use
$collectionHotel->load();
this error shows up:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘apptha_booking_room_types.room_price_per_night’ in ‘having clause’
I tried everything I could but I can't solve this problem yet. Can anyone help?
Alternatively, are there better ways to implement the having() function?