Michał Wejwoda Michał Wejwoda - 7 months ago 35
PHP Question

Automatic activation dataflow profile by cron

I want to run my dataflow import profile by cron without manipulation in Cron.php or crontab files. My profile is working, when I start it manually.
This adapter imports data about product from csv file.
I'm trying to run this profile by :

$profile->load($profileId);
$profile->run();


But I have no effect after cron action. Run method isn't work correctly.
File is in the correct location. Id of profile is correct, profile was loaded.

Answer

I found answer. If you want to run import profile, you should use this code after $profile->run(); :

        $batchModel = Mage::getSingleton('dataflow/batch');
        $batchImportModel = $batchModel->getBatchImportModel();
        $adapter = $batchModel->getAdapter();
        $importIds = $batchImportModel->getIdCollection();

        foreach ($importIds as $importId) {
            $batchImportModel->load($importId);
            try {
                $importData = $batchImportModel->getBatchData();
                Mage::getModel($adapter)->saveRow($importData);
            } catch (Exception $e) {
                Mage::log("Exception : " . $e);
                continue;
            }
        }

        if (method_exists($adapter, 'getEventPrefix')) {

            // Event to process rules relationships after import
            Mage::dispatchEvent($adapter->getEventPrefix() . '_finish_before', array(
                'adapter' => $adapter
            ));

            // Clear affected ids for possible reuse
            $adapter->clearAffectedEntityIds();

        }