1

您好我想知道如何编写 php 语句来执行一些 sql 查询。

例如:(列出所有价格最低的产品)

$collection1 = Mage::getModel('catalog/product')->getCollection();
$collection1
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('attribute_set_id', 4)
->setOrder('created_at', 'desc')
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents();

sql查询是这样的:

SELECT `e`.*, `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` 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`.`attribute_set_id` = '4') ORDER BY `e`.`created_at` desc

但我知道以下 php 语句可以列出查看次数最多的产品:

$collection2 = Mage::getResourceModel('reports/product_collection');
$collection2->addViewsCount();

然后,sql查询是这样的:

SELECT COUNT(report_table_views.event_id) AS `views`, `e`.* FROM `report_event` AS `report_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4
WHERE (report_table_views.event_type_id = 1) GROUP BY `e`.`entity_id` HAVING 
(COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC

我正在考虑如何编写用于组合上述两个 sql 查询的 php 语句,我想显示以下 sql 查询的结果。

SELECT COUNT(report_table_views.event_id) AS `views`, `e`.*, `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 `report_event` AS `report_table_views` INNER JOIN 
`catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4  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 (report_table_views.event_type_id = 1 AND `e`.`attribute_set_id` =
'4') GROUP BY `e`.`entity_id` HAVING (COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC

我以为我可以编写以下 php 语句,但它不起作用,也许 addViewsCount() 是覆盖整个 sql 语句。

$collection3 = Mage::getResourceModel('reports/product_collection');

$collection3
->addAttributeToSelect('*')
->addAttributeToFilter('attribute_set_id', 4)
->addOrderedQty()
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addViewsCount(); 

现在,谁能知道如何编写 php 语句以列出以最低价格查看最多的产品,请原谅我有一个麻烦的要求,即所有产品实体的属性集 id 必须为 4。我知道这个问题非常具有挑战性,因为我不知道从谷歌搜索此类信息。请大家看问题。感谢您的每一次努力。

4

2 回答 2

4

尝试类似的东西

$productCount = 5;

//Get Store ID

$storeId = Mage::app()->getStore()->getId();      

// Get most viewed product collection

$products = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')     
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addAttributeToSelect(array('name', 'price', 'minimal_price', 'small_image')) 
    ->addViewsCount()
    ->setPageSize($productCount);

Mage::getSingleton('catalog/product_status')
        ->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')
        ->addVisibleInCatalogFilterToCollection($products);

print_r($products->getData());

阅读更多:

于 2012-11-03T16:44:51.917 回答
1
<?php $productCount = 5;
 $storeId = Mage::app()->getStore()->getId(); 
$products = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addViewsCount() ->setPageSize($productCount); Mage::getSingleton('catalog/product_status') ->addVisibleFilterToCollection($products); 
Mage::getSingleton('catalog/product_visibility') ->addVisibleInCatalogFilterToCollection($products);
 print"<pre>";
 print_r($products); ?>
于 2014-04-21T06:09:47.370 回答