目前我正在使用拍卖模块的网上商店工作。这个拍卖模块有它自己的一组实体,代表拍卖和它们的出价。主要实体是product模型。此模型与Magento的Catalog_Product模型有关联。
无论在哪里加载集合,产品都必须在ProductAuctions集合加载之后才加载。
现在我必须编写一些奇异的查询来加载特定的拍卖集,并结合类别和搜索查询。现在,我首先必须加载属于给定类别和搜索查询的产品集合,然后加载属于相应产品集合的活动拍卖。
在某些情况下,我不能重用已加载的产品集,然后必须执行另一个查询来加载与拍卖相对应的产品。
最坏的情况下,我将不得不执行三个大查询,并处理一个查询中可能出现的结果集。
在Magento中是否有可能在集合中加载相关实体,就像任何体面的ORM会处理One-2-One, Many-2-One和其他关系一样?
我没有找到任何这样的例子,但我无法想象这在Magento中是不可能的。
谢谢你的帮助。
== EDIT ==
一段示例代码来显示我正在做的事情:
/**
* 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;
}
设法想出了一个解决方案。现在,我使用以下代码来获取所需的所有信息。我仍然感到惊讶的是,像任何普通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;
}