1

こんにちは、いくつかの SQL クエリを実行するための php ステートメントの書き方を知りたいです。

例: (最小価格のすべての製品をリストする)

$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

上記の 2 つの 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 ステートメントの書き方を誰が知っているでしょうか。Google からそのような情報を検索する方法がわからないので、この質問は非常に難しいことを知っています。みなさん、問題を見てください。いつもありがとうございます。

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 に答える