Last active
August 28, 2017 06:06
-
-
Save Amakata/ff2b0be55e7706ec082c48c9b8b88021 to your computer and use it in GitHub Desktop.
Magentoで商品Collectionでmemory limitを超えてしまったら
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
$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