2011-09-23 83 views
0

當前我正在使用拍賣模塊在網上商店工作。這個拍賣模塊有自己的一套代表拍賣和他們的出價的實體。主要實體是ProductAuction模型。該模型與Magento的Catalog_Product模型有關係。加載相應的實體,同時加載集合

無處不在收集產品必須加載ProductAuctions收集後加載。

現在我不得不寫一些異國情調的查詢來加載特定的拍賣組合與類別和搜索查詢。現在,我首先必須加載屬於給定類別和搜索查詢的產品集合,然後加載屬於相應產品集合的活動拍賣。

在某些情況下,我無法重新使用加載的產品集,然後必須執行另一個查詢來加載相應的拍賣產品。

最糟糕的情況我將不得不執行三個大的查詢並處理在一個查詢中應該可能的結果集。

是否有可能在Magento中加載集合中的相關實體,就像任何體面的ORM會與One-2-One,Many-2-One和其他關係一樣?

我還沒有找到任何這方面的例子,但我無法想象這在Magento中是不可能的。

感謝您的任何幫助。

== ==編輯

的示例代碼有點展現我此刻在做什麼:

/** 
* Build the correct query to load the product collection for 
* either the search result page or the catalog overview page 
*/ 
private function buildProductCollectionQuery() { 

    /** 
    * @var Mage_CatalogSearch_Model_Query 
    */ 
    $searchQuery = Mage::helper('catalogsearch')->getQuery(); 

    $store_id = Mage::app()->getStore()->getId(); 
    $productIds = Mage::helper('auction')->getProductAuctionIds($store_id); 

    $IDs = array(); 
    $productIds = array(); 
    $collection = Mage::getModel('auction/productauction') 
          ->getCollection() 
          ->addFieldToFilter(
               'status', array('in' => array(4)) 
              ); 

    if (count($collection)) { 
     foreach ($collection as $item) { 
      $IDs[] = $item->getProductId(); 
     } 
    } 

    $collection = Mage::getResourceModel('catalog/product_collection') 
             ->addFieldToFilter('entity_id',array('in'=> $IDs)) 
             ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) 
             ->addMinimalPrice() 
             ->addTaxPercents() 
             ->addStoreFilter(); 

    if($searchQuery != null) { 
     $collection->addAttributeToFilter(array(
           array('attribute' => 'name', 'like'=>'%' . $searchQuery->getQueryText() . '%'), 
           array('attribute' => 'description', 'like'=>'%' . $searchQuery->getQueryText() . '%') 
            ) 
           ); 

     // @TODO This should be done via the Request object, but the object doesn't see the cat parameter 
     if(isset($_GET['cat']) ) { 
       $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat'])); 
     } 
    } 

    return $collection; 
} 
+0

有沒有你不使用任何連接特定的原因是什麼? –

+0

已經嘗試了幾種方法,並且儘管連接工作正常,但我只能將其作爲數據數組而不是代表對象。我需要域模型對象... – pderaaij

+3

好吧,如果你需要它們,你必須加載所有的對象,以正確的方式加入它們會給你所有必需的起點 –

回答

0

託管拿出一個解決方案。我現在使用下面的代碼來獲取我需要的所有信息。我仍然很驚訝,創建相關對象的實例非常困難,就像任何常規的ORM會做的那樣。但是,也許我期待太多的Magento ..

無論如何,這是我現在使用的代碼:

/** 
* Build the correct query to load the product collection for 
* either the search result page or the catalog overview page 
*/ 
private function buildProductCollectionQuery() { 
    /** 
    * @var Mage_CatalogSearch_Model_Query 
    */ 
    $searchQuery = Mage::helper('catalogsearch')->getQuery(); 

    $collection = Mage::getResourceModel('catalog/product_collection') 
             ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) 
             ->addAttributeToSelect(array('product_id' => 'entity_id')) 
             ->joinTable(array('productauction' => 'auction/productauction'), 'product_id=entity_id', array('start_time' => 'productauction.start_time','end_time' => 'productauction.end_time','entity_id' => 'productauction.productauction_id', 'product_id' => 'product_id')) 
             ->joinTable(array('auction' => 'auction/auction'), 'product_id = entity_id', array('last_bid' => 'MAX(auction.price)'), 'auction.productauction_id = productauction.productauction_id', 'inner') 
             ->addMinimalPrice() 
             ->addTaxPercents() 
             ->addStoreFilter() 
             ->setPage((int) $this->getRequest()->getParam('p', 1), 1); 

    $currentCategory = Mage::registry('current_category'); 
    if($currentCategory != null) { 
     $collection->addCategoryFilter($currentCategory); 
    } 

    if($searchQuery != null) { 
     $collection->addAttributeToFilter(array(
           array('attribute' => 'name', 'like'=>'%' . $searchQuery->getQueryText() . '%'), 
           array('attribute' => 'description', 'like'=>'%' . $searchQuery->getQueryText() . '%') 
            ) 
           ); 

     // @TODO This should be done via the Request object, but the object doesn't see the cat parameter 
     if(isset($_GET['cat']) ) { 
       $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat'])); 
     } 
    } 

    $collection->getSelect()->group('productauction.productauction_id'); 
    return $collection; 
} 
相關問題