如何在magento中创建可配置的cron计划?

时间:2016-08-02 17:02:34

标签: php xml magento cron

我一直在尝试通过管理站点在Magento中创建可配置的Cron计划。我读到如果未在config.xml中指定cron计划,Magento会在core_config_data表中搜索它。 我能够填充core_config_data表,但是没有创建计划。 这是我的文件结构 [该模块是Saeconnector,位于app / code / local / Wolfsellers / Saeconnector] [1]

这是我的config.xml文件:

<config>
<tabs>
    <saeconnector translate="label" module="saeconnector">
        <label>Wolfsellers</label>
        <sort_order>5</sort_order>
    </saeconnector>
</tabs>
<sections>
    <saeconnector_options translate="label" module="saeconnector">
        <label>Configuration</label>
        <tab>saeconnector</tab>
        <frontend_type>text</frontend_type>
        <sort_order>1</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>0</show_in_website>
        <show_in_store>0</show_in_store>

        <groups>    
            <db_conf translate="label">
                <label>SAE-Database Configuration</label>
                <frontend_type>text</frontend_type>
                <sort_order>1</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>                
                <fields>
                    <user>
                        <label>Login user:</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <comment>SAE database login user.</comment>                   
                    </user>
                    <login>
                        <label>Login password:</label>
                        <frontend_type>password</frontend_type>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <comment>SAE database login password.</comment>                   
                    </login>
                    <path>
                        <label>Database URL:</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <comment>SAE database URL.</comment>                   
                    </path>

                </fields>
            </db_conf>
            <configurable_cron translate="label">
                <label>Configuration - Sync</label>
                <frontend_type>text</frontend_type>
                <sort_order>2</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>0</show_in_website>
                <show_in_store>0</show_in_store>                
                <fields>
                   <enable_sync translate="label">
                        <label>Enable</label>
                        <frontend_type>select</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <source_model>wolfsellers_saeconnector/yesno</source_model>
                    </enable_sync>


                    <time translate="label">
                        <label>Start Time</label>
                        <frontend_type>time</frontend_type>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                       <depends>
                            <enable_sync>1</enable_sync>
                        </depends>
                    </time>



                    <frequency translate="label">
                        <label>Frequency</label>
                        <frontend_type>select</frontend_type>
                        <source_model>adminhtml/system_config_source_cron_frequency</source_model>
                        <backend_model>wolfsellers_saeconnector/system_config_backend_cron</backend_model>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <depends>
                            <enable_sync>1</enable_sync>
                        </depends>
                    </frequency>

                    <recipient translate="label">
                        <label>Error Email Recipient</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>4</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <depends>
                            <enable_sync>1</enable_sync>
                        </depends>
                    </recipient>
                    <sender translate="label">
                        <label>Error Email Sender</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>5</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <depends>
                            <enable_sync>1</enable_sync>
                        </depends>
                    </sender>
                </fields>
            </configurable_cron>
            <inv_conf translate="label">
                <label>Inventario</label>
                <frontend_type>text</frontend_type>
                <sort_order>3</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>0</show_in_website>
                <show_in_store>0</show_in_store>                
                <fields>
                    <enable_sync_inv>
                        <label>Enable sync inventory</label>
                        <frontend_type>select</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <source_model>wolfsellers_saeconnector/yesno</source_model>
                    </enable_sync_inv>
                    <inv_attr>
                        <label>Attributes to sync</label>
                        <frontend_type>multiselect</frontend_type>
                        <source_model>wolfsellers_saeconnector/productattr</source_model>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <depends>
                            <enable_sync_inv>1</enable_sync_inv>
                        </depends>
                    </inv_attr>

                </fields>
            </inv_conf>

            <client_conf translate="label">
                <label>Clientes</label>
                <frontend_type>text</frontend_type>
                <sort_order>4</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>0</show_in_website>
                <show_in_store>0</show_in_store>                
                <fields>
                    <enable_sync_client>
                        <label>Enable sync client</label>
                        <frontend_type>select</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <source_model>wolfsellers_saeconnector/yesno</source_model>
                    </enable_sync_client>
                    <client_attr>
                        <label>Attributes to sync</label>
                        <frontend_type>multiselect</frontend_type>
                        <source_model>wolfsellers_saeconnector/clientattr</source_model>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <depends>
                            <enable_sync_client>1</enable_sync_client>
                        </depends>
                    </client_attr>
                </fields>
            </client_conf>
        </groups>                
    </saeconnector_options>

</sections>

这是我的system.xml

class Wolfsellers_Saeconnector_Model_System_Config_Backend_Cron extends Mage_Core_Model_Config_Data{
const CRON_STRING_PATH  = 'crontab/jobs/wolfsellers_saeconnector/schedule/cron_expr';
const CRON_MODEL_PATH   = 'crontab/jobs/wolfsellers_saeconnector/run/model';

protected function _afterSave()
{
    $enabled     = $this->getData('groups/configurable_cron/fields/enable_sync/value');
    $frequncy    = $this->getData('groups/configurable_cron/fields/frequency/value');
    $time        = $this->getData('groups/configurable_cron/fields/time/value');



    $frequencyDaily     = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_DAILY;
    $frequencyWeekly    = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_WEEKLY;
    $frequencyMonthly   = Mage_Adminhtml_Model_System_Config_Source_Cron_Frequency::CRON_MONTHLY;
    $cronDayOfWeek      = date('N');

    $cronExprArray      = array(
        intval($time[1]),                                   # Minute
        intval($time[0]),                                   # Hour
        ($frequncy == $frequencyMonthly) ? '1' : '*',       # Day of the Month
        '*',                                                # Month of the Year
        ($frequncy == $frequencyWeekly) ? '1' : '*',         # Day of the Week
    );






    $cronExprString = join(' ', $cronExprArray);

    try {
        Mage::getModel('core/config_data')
            ->load(self::CRON_STRING_PATH, 'path')
            ->setValue($cronExprString)
            ->setPath(self::CRON_STRING_PATH)
            ->save();
        Mage::getModel('core/config_data')
            ->load(self::CRON_MODEL_PATH, 'path')
            ->setValue((string) Mage::getConfig()->getNode(self::CRON_MODEL_PATH))
            ->setPath(self::CRON_MODEL_PATH)
            ->save();

        Mage::getConfig()->cleanCache();
    } catch (Exception $e) {
        throw new Exception(Mage::helper('cron')->__('Unable to save the cron expression.'));
    }
}

这是我的Cron.php文件

id

}

我的数据库core_config_date设置如下:

[这两行是由Cron.php创建的] [2]

http://i.stack.imgur.com/2EYsB.png http://i.stack.imgur.com/bQHIx.png

我只是想创建一个可配置的cron时间表,如果有另一种方式或我做错了什么,可以帮助或引导我走上真理和严谨的道路,请提前感谢。

0 个答案:

没有答案