Skip to content

Instantly share code, notes, and snippets.

@Amakata
Last active August 28, 2017 06:06
Show Gist options
  • Save Amakata/ff2b0be55e7706ec082c48c9b8b88021 to your computer and use it in GitHub Desktop.
Save Amakata/ff2b0be55e7706ec082c48c9b8b88021 to your computer and use it in GitHub Desktop.
Magentoで商品Collectionでmemory limitを超えてしまったら
```
$handler = fopen($file, 'w+');
$encoding = 'SJIS';
$attributeCodes = array('price','name');
$collection = Mage::getResourceModel('catalog/product_collection');
// * はjoinしないので使えない $collection->addAttributeToSelect('*');
foreach ($attributeCodes as $attributeCode) {
// $collection->addAttributeToSelect($attributeCodes, 'left'); は1.9では使えない
$collection->addAttributeToSelect($attributeCode, 'left'); // leftにしないと属性が無い場合に取得できない
}
// 低レベルのイテレータでアクセス
Mage::getSingleton('core/resource_iterator')->walk(
$collection->getSelect(),
array(array($this, 'writeCsvCallback')),
array('encoding' => $encoding, 'handler' => $handler));
public function writeCsvCallback($args) {
$p = Mage::getModel('catalog/product');
$p->setData($args['row']);
$data = array(
$p->getSku(),
$p->getName(),
$p->getPrice()
);
$this->writeCsvData($args['handler'], $data, $args['encoding']);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment