Как зациклить коллекцию Magento? - PullRequest
4 голосов
/ 24 сентября 2010

По сути, мне нужно получить CSV-файл моих клиентов, автоматически генерируемый в сценарии каждый день. Я пробовал несколько способов, но они слишком медленные или фактически исчерпаны из памяти.

* 1) foreach через ресурс коллекции *

$collection = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left')
->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

foreach($collection as $customer) {
echo $customer->getFirstname() . ",";
}

2) foreach и load customer

$collection = Mage::getResourceModel('customer/customer_collection');
foreach($collection as $customer) {
  $fullcustomer = Mage::getModel("customer/customer")->load($customer->getId());
  echo $fullcustomer->getFirstname() . ",";
}

Есть идеи?

Спасибо!

Ответы [ 2 ]

12 голосов
/ 04 декабря 2012

Попробуйте листать большую коллекцию!

Идея в том, что если вы можете загружать коллекцию небольшими порциями, вы не будете использовать столько памяти. Загрузите блок (страницу), затем сделайте с ним что-нибудь, например, сохраните его в текстовом файле, а затем загрузите следующий блок. В результате вы работали со всей большой коллекцией, но понесли затраты на память самой большой страницы.

Мы используем нечто подобное для экспорта заказов из нашего магазина. Я подключил вашу коллекцию, и она, кажется, работает.

<?php

 if(php_sapi_name()!=="cli"){
 echo "Must be run from the command line.";
 };

/**
 * Setup a magento instance so we can run this export from the command line.
 */

require_once('app/Mage.php');
umask(0);

if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); error_reporting(E_ALL);

try {
    Mage::getConfig()->init();
    Mage::app();   
} catch (Exception $e) {
    Mage::printException($e);
}
ini_set('memory_limit','500M');

$customerCount = 0;
try{
    //configure the collection filters.
    $collection = Mage::getResourceModel('customer/customer_collection')
    ->addAttributeToSelect('email')
    ->addAttributeToSelect('created_at')
    ->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left')
    ->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
    ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
    ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
    ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
    ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
    ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

    //Add a page size to the result set.
    $collection->setPageSize(100);
    //discover how many page the result will be.
    $pages = $collection->getLastPageNumber();
    $currentPage = 1;
    //This is the file to append the output to.
    $fp = fopen('/tmp/customers.csv', 'w');
    do{
         //Tell the collection which page to load.
         $collection->setCurPage($currentPage);
         $collection->load();
         foreach ($collection as $customer){
            //write the collection array as a CSV.
            $customerArray = $customer->toArray();
            //var_dump($customerArray); echo "\n\n";
            fputcsv($fp, $customerArray);
            $customerCount++;
         }
         $currentPage++;
         //make the collection unload the data in memory so it will pick up the next page when load() is called.
         $collection->clear();
    } while ($currentPage <= $pages);
    fclose($fp);
} catch (Exception $e) {
    //$response['error'] = $e->getMessage();
    Mage::printException($e);
}
echo "Saved $customerCount customers to csv file \n";
?>

Я сохранил его как exportCustomers.php.

Затем настройте задачу cron для запуска: php -f /path/to/your/magento/exportCustomers.php

4 голосов
/ 27 сентября 2010

Использование load() для каждого объекта Customer приведет к значительному замедлению вашего кода . Я бы предположил, что ваш первый подход к добавлению обязательных атрибутов в коллекцию является правильным, но завершите его чем-то вроде:

$collection->toArray([$arrRequiredFields = array()]);

Таким образом, отдельные клиенты никогда не загружаются, но toArray() ( здесь ) даст вам нужные поля, а затем вы можете перебрать многомерный массив, чтобы получить запятую. разделенная строка

В качестве альтернативы, вы можете попробовать $collection->toXml() и работать с XML, если вам это удобно. Google укажет вам на методы преобразования xml-> csv.

...