2012-11-12 15 views
3

我需要Magento中的「有」SQL函數。據我所知,沒有功能。在Magento中實現具有()的SQL函數

所以我嘗試實現它在集合類

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「having子句中'

我盡我所能,但我還是無法解決這個問題。誰能幫忙?

另外,是否有更好的方法來實現having()函數?

回答

0

嘗試對DB手動運行SQL和改變這一行

.... 
HAVING (MIN(`apptha_booking_room_types`.`room_price_per_night`) >= 0) 
    AND (MIN(`apptha_booking_room_types`.`room_price_per_night`) <= 999999999) 

..... 
HAVING (`lowestprice` >= 0) 
    AND (`lowestprice` <= 999999999) 

如果上面的代碼工作,然後嘗試改變這種

->addAttributeHaving('`lowestprice` >= '.$lowestprice) 
->addAttributeHaving('`lowestprice` <= '.$highestprice); 
+1

你好..三江源答案.. 我已經改變的查詢分成 HAVING('lowestprice'> = 0)AND('lowestprice' <= 999999999) 輸出是在MySQL 但在Magento良好錯誤是相同的 SQLSTATE [42S22]:未找到列:1054'having clause'中的未知列'lowestprice' – sayasemut

+0

它現在產品是什麼SQL? –

+0

輸出sql是好的,nothings錯誤..當我嘗試在mysql中執行輸出,它的工作原理.. – sayasemut

2

我的信到目前爲止,這是一個magento的bug .. 好吧...我的問題解決了b y使用一些技巧..但不使用這種方法,因爲magento總是說未知的列,雖然輸出查詢是好的。

+0

嗨,我需要添加不必要我的查詢了,請問,你發現了哪個解決方案?謝謝。 – Kostanos

+0

hi kostanos,sry我剛剛讀了你的消息,我發現了另一種方法來實現擁有,簡單的方法,獲得你的集合,並使用這個函數$ collection-> getSelect() - > having($ attribute);希望這個幫助... – sayasemut

0

另一種方式寫有秩序條款

$product_log_data = Mage::getModel('customtabs/productlog')->getCollection(); 
$product_log_data->getSelect()->having(
      "rowNumber = 1"); 
$product_log_data->getSelect()->order(
      "rowNumber"); 
0

不幸的是,這是在Magento的錯誤。我有同樣的問題,並最終創建了第二個數據庫模型類,僅用於檢索管理區域網格顯示的數據。

在第二個Collection.php文件的構造函數方法中,我最終創建了一個使用直接DB查詢的mysql VIEW,從原始表中選擇數據(使用group by子句)以臨時緩存結果。

在構造方法:

  $resource = Mage::getSingleton('core/resource'); 
      $writeConnection = $resource->getConnection('core_write'); 

      $sql = " 
        CREATE OR REPLACE VIEW your_temp_view_name AS 
        SELECT SUM(column_name) as column_name 
        FROM original_table_name 
        WHERE ... 
        GROUP BY customer_id 
      "; 

      $writeConnection->query($sql); 

因爲數據已經分組和彙總功能已經被執行了,然後你可以只用一般的方法addAttributeToFilter過濾數據。

這有點不好意思,但它在這種情況下工作。取決於數據量和同時用戶數量,它可能會或可能不會工作。

0

首先$countSelect->reset(Zend_Db_Select::HAVING);表示它將重置HAVING從您的收藏。這意味着它將刪除擁有條款。而不是你想要的。您可能要將其添加到集合中(app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php->_getSelectCountSql()這裏。)

但主要的元兇getSize()方法存在於lib/Varien/Data/Collection/Db.php文件中。

我嘗試了幾乎所有可用的互聯網解決方案,但他們都沒有爲我工作。

現在我做了以下。

public function getSize() 
{ 
    if (is_null($this->_totalRecords)) { 
     //$sql = $this->getSelectCountSql(); 
     $sql = $this->getSelect(); 
     $this->_totalRecords = count($this->getConnection()->fetchAll($sql, $this->_bindParams)); 
    } 
    return intval($this->_totalRecords); 
} 

檢查我甚至沒有使用getSelectCountSql()。我只是讀取整個SQL QUERY取回所有數據返回計數它。就這樣。