简体   繁体   English

Magento:以编程方式获取产品列表

[英]Magento: Getting list of products programmatically

I'm trying to get a list of products from my Magento store, and I only need three attributes loaded per product, which, I figure, would take a lot less time than if I were to load the whole product. 我正在尝试从Magento商店中获取产品列表,并且我只需要为每个产品加载三个属性,我认为,与加载整个产品相比,这将花费更少的时间。 Now, either I'm not doing it right, or, I'm wrong assuming above. 现在,或者我做错了,或者我做错了上述假设。 Here's my script: 这是我的脚本:

<?php
$script_start = microtime(true);

// Load Magento core
require_once '../app/Mage.php';
Mage::app();

// Load products :: collection

$query_create_start = microtime(true);

$prod_catalog = Mage::getModel("catalog/product")->getCollection()->
            addAttributeToSelect('sku')->
            addAttributeToSelect('name')->
            addAttributeToSelect('category_ids');

$query_create_end = microtime(true);

$query_create_time = $query_create_end - $query_create_start;

echo "Query creation took $query_create_time seconds.<br>";

$product_loop_start = microtime(true);
foreach($prod_catalog as $product) {
    var_dump($product);
    $one_product_loop = microtime(true);
    $one_product_time =  $one_product_loop - $product_loop_start;
    echo "Getting one product took $one_product_time seconds.<";
    die('done');
}
$product_loop_end = microtime(true);

?>

And here's output(without dump data): 这是输出(没有转储数据):

Query creation took 0.0043220520019531 seconds.
one product took 32.509027004242 seconds

Now, when I take a look at the product var_dump, I see that it loaded more attributes than I asked: 现在,当我看一下产品var_dump时,发现它加载的属性比我问的要多:

object(Mage_Catalog_Model_Product)#74 (32) {
  ["_cacheTag":protected]=>
  string(15) "catalog_product"
  ["_eventPrefix":protected]=>
  string(15) "catalog_product"
  ["_eventObject":protected]=>
  string(7) "product"
  ["_canAffectOptions":protected]=>
  bool(false)
  ["_typeInstance":protected]=>
  NULL
  ["_typeInstanceSingleton":protected]=>
  NULL
  ["_linkInstance":protected]=>
  NULL
  ["_customOptions":protected]=>
  array(0) {
  }
  ["_urlModel":protected]=>
  NULL
  ["_errors":protected]=>
  array(0) {
  }
  ["_optionInstance":protected]=>
  NULL
  ["_options":protected]=>
  array(0) {
  }
  ["_reservedAttributes":protected]=>
  NULL
  ["_isDuplicable":protected]=>
  bool(true)
  ["_calculatePrice":protected]=>
  bool(true)
  ["_defaultValues":protected]=>
  array(0) {
  }
  ["_storeValuesFlags":protected]=>
  array(0) {
  }
  ["_lockedAttributes":protected]=>
  array(0) {
  }
  ["_isDeleteable":protected]=>
  bool(true)
  ["_isReadonly":protected]=>
  bool(false)
  ["_resourceName":protected]=>
  string(15) "catalog/product"
  ["_resource":protected]=>
  NULL
  ["_resourceCollectionName":protected]=>
  string(26) "catalog/product_collection"
  ["_dataSaveAllowed":protected]=>
  bool(true)
  ["_isObjectNew":protected]=>
  NULL
  ["_data":protected]=>
  array(12) {
    ["entity_id"]=>
    string(1) "7"
    ["entity_type_id"]=>
    string(1) "4"
    ["attribute_set_id"]=>
    string(1) "4"
    ["type_id"]=>
    string(6) "simple"
    ["sku"]=>
    string(10) "1000000000"
    ["has_options"]=>
    string(1) "0"
    ["required_options"]=>
    string(1) "0"
    ["created_at"]=>
    string(19) "2016-07-11 11:05:03"
    ["updated_at"]=>
    string(19) "2017-03-03 21:14:21"
    ["name"]=>
    string(62) "My simple product"
    ["is_salable"]=>
    string(1) "1"
    ["stock_item"]=>
    object(Varien_Object)#49 (7) {
      ["_data":protected]=>
      array(1) {
        ["is_in_stock"]=>
        string(1) "1"
      }
      ["_hasDataChanges":protected]=>
      bool(false)
      ["_origData":protected]=>
      NULL
      ["_idFieldName":protected]=>
      NULL
      ["_isDeleted":protected]=>
      bool(false)
      ["_oldFieldsMap":protected]=>
      array(0) {
      }
      ["_syncFieldsMap":protected]=>
      array(0) {
      }
    }
  }
  ["_hasDataChanges":protected]=>
  bool(true)
  ["_origData":protected]=>
  NULL
  ["_idFieldName":protected]=>
  string(9) "entity_id"
  ["_isDeleted":protected]=>
  bool(false)
  ["_oldFieldsMap":protected]=>
  array(0) {
  }
  ["_syncFieldsMap":protected]=>
  array(0) {
  }
}

Any ideas? 有任何想法吗?

When system load one product, it will load all static attributes by default. 系统加载一个产品时,默认情况下它将加载所有静态属性。

Static attribute is stored in main entity table "catalog_product_entity", other attributes are stored in related table according the backen_type of this attribute. 静态属性存储在主实体表“ catalog_product_entity”中,其他属性根据该属性的bac​​ken_type存储在相关表中。 For example, 'name" is stored in 'catalog_product_entity_varchar'. 例如,“名称”存储在“ catalog_product_entity_varchar”中。

addAttributeToSelect method only impact non-static attributes, So in your case you can see so many static attributes that have been load. addAttributeToSelect方法仅影响非静态属性,因此,在您的情况下,您可以看到已加载的许多静态属性。

You can try below code: 您可以尝试以下代码:

$prod_catalog = Mage::getModel("catalog/product")->getCollection();          
$prod_catalog->getSelect()->reset("columns")->columns(array("sku","entity_id"));
$prod_catalog->addAttributeToSelect('name');
foreach($prod_catalog as $product) {
 $category_ids =  $product->getCategroyIds();
 ###you code
}

Tips:though category_ids is static, but there is no column in main entity table as well as other product entity table, so it make no sense to run addAttributeToSelect('categroy_ids') or add to columns function, category ids data is from another table catalog_category_product") 提示:尽管category_ids是静态的,但是主实体表以及其他产品实体表中没有列,所以运行addAttributeToSelect('categroy_ids')或添加到列函数没有意义,类别ID数据来自另一个表catalog_category_product”)

您可以尝试将Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, $atrributeCode, $storeId)用作单个属性,而不是getCollection() ,它可能会更快地满足您的需求。

you have the flat products enabled. 您已启用平板产品。 The flat tables generated by the flat product indexer contain only active products. 平面产品索引器生成的平面表仅包含有效产品。 This is for performance reasons. 这是出于性能原因。 There is no need to index the disabled products since you are not going to use them on front end. 无需索引禁用产品,因为您不会在前端使用它们。 (and the flat tables are used only on front-end). (并且平板仅在前端使用)。

If you are trying to get your products in a custom script and you need to get the disabled ones then you can do this. 如果您尝试使用自定义脚本获取产品,并且需要获取禁用的脚本,则可以执行此操作。

$collection = Mage::getResourceModel('catalog/product_collection') ->...add filters here. $ collection = Mage :: getResourceModel('catalog / product_collection')-> ...在此处添加过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM