【问题标题】:Getting a CSV files and setting it to automatically upload to magento获取 CSV 文件并将其设置为自动上传到 magento
【发布时间】:2014-08-09 19:26:51
【问题描述】:

假设一个文件会自动更新产品或客户,现在我们需要一个脚本来将文件上传到 magento 并每天运行。我有要上传的代码,但我有两个问题。

  1. 我们如何将此脚本设置为自动运行?...Cron?
  2. 有比这个脚本更好的方法吗?

       if ($_FILES[csv][size] > 0) 
          { 
          // the table fields
          $tbl_fields = array('customerCode', 'postCode,Name', 'Address1', 'Address2');
          //get the csv file 
          $file = $_FILES[csv][tmp_name]; 
          $handle = fopen($file,"r"); 
          // get the first line into a fields map
          $csv_fields = fgetcsv($handle,1000,",",'"');
          // we will insert only common fields
          $tbl_fields = array_intersect($tbl_fields,$csv_fields);
          // if there's not at least one common field, don't go on
          if(count($tbl_fields)>0)
            {
            // we need the table's field names as keys (see below)
            $tbl_fields = array_flip($tbl_fields);
           // now let's go after the data
            while($data = fgetcsv($handle,1000,",",'"'))
              {
              $data=array_map('addslashes',$data); // apply addslashes() to all values 
              $data=array_combine($csv_fields,$data); // csv fields assoc (key=>value)
              $data=array_intersect_key($data,$tbl_fields); // discard redundant
              $tbl_fields_str=implode("`,`",array_keys($data));
              $tbl_vals_str=implode("','",array_values($data));
              $q="INSERT INTO `PowerFlex` (`$tbl_fields_str`) VALUES ('$tbl_vals_str')";
              mysql_query($q);
              }
            }
    
     }
    

【问题讨论】:

    标签: php sql magento csv


    【解决方案1】:

    我会以“是”来回答这两个问题。使用 Magento 的 Cron(它基本上是普通 cron 守护程序的包装器),设置非常简单,只需将这些行添加到您的配置中:

    <config>
    ...
        <crontab>
            <jobs>
                <unique_identifer>
                    <schedule>
                        <cron_expr>0 2 * * *</cron_expr>
                    </schedule>
                    <run>
                        <model>your_module/model::method</model>
                    </run>
                </unique_identifer>
            </jobs>
        </crontab>
    </config>
    

    节点 cron_expr 告诉 magento 何时运行,当前设置为每天凌晨 2 点。 model::method 是它将执行的方法。您可以在此处找到有关 cron 以及如何设置 magento 以使用您的 linux cron 选项卡的更多信息:http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job

    为了检查你的 crontab 是否正在运行,有一个 SO 问题:Magento: monitoring execution time of all cron jobs?

    关于你的第二个问题: 很难说,是否有更好的方法,我可能会设置一个模型来表示将要插入的数据。所以我肯定会将 INSERT INTO 行更改为:

    Mage::getModel('your_module/model')->setCustomerCode($customerCode)
         ->setPostCode(...)
         ->setName(...)
         ...
         ->save();
    

    这使您能够对插入的值强制一些约束(如果需要),还可以使用 Magento 框架(带有日志记录、错误处理等)。

    有关如何创建模型的更多信息,请参阅 magento 的这篇博文: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics

    HTH!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2012-05-25
      • 2016-03-03
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多