2011-09-23 65 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; 
} 
相关问题