加载集合时加载相应的实体

时间:2011-09-23 12:47:53

标签: php magento

当前我正在使用拍卖模块在网上商店工作。该拍卖模块具有自己的一组实体,代表拍卖及其出价。主要实体是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;
}

1 个答案:

答案 0 :(得分: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;
}