首页 > 解决方案 > 调度 Windows 作业以运行 PHP 脚本失败

问题描述

我有一个 PHP 脚本,可以将 SQL 表中的数据导出到 Drupal 内容中。我想将作业安排为 Windows 任务并按照以下步骤操作:

  1. 我的 PHP 文件包含以下代码:

 <?php
     namespace Drupal\Import\Commands; 
     use Drush\Commands\DrushCommands;
     use Drupal\node\Entity\Node;
    
    class ImportCommands extends DrushCommands 
    {
        public function loadData() 
    	{
            $tx = \Drupal::database()->startTransaction();
            try 
    		{
                $query_result = \Drupal::entityQuery('node')
                                    ->condition('type', 'test_mod')
                                    ->execute();
                entity_delete_multiple('node', $query_result);
    			
                $database = \Drupal::database();
                $result = $database->query("SELECT * FROM my_sql_table");
                $records = $result->fetchAll();
       
          
                foreach($records as $key => $record) {
                    $node = Node::create(['type' => 'test_mod']);
                    $node->set('title', $key);
    				$node->set('field_IDVal', $record->ID);
    				$node->set('field_name', $record->Name);
                    $node->status = 1;
                    $node->enforceIsNew();
                    $node->save();
                }
    			
            } 
    		catch (Exception $e) {
                $tx->rollBack();
                // log/report failure
            }
    
            // $tx->commit();
        }
    
    }

  1. 创建 .bat 文件并具有以下查询:

调用 C:\MyProj\web\Commands\php.exe -f C:\MyProj\web\Commands\ExportCommands.php

  1. 创建了一个 windows 任务来链接这个文件。

我所看到的只是任务成功的 0x1。任何帮助使这项工作?

预期行为:我想创建一个调用此 PHP 的 wndows 任务,并且文件中的命令将数据从 SQL 表加载到 Drupal。

现在我通过命令提示符执行文件并执行: drush MyProj:loaddata

有什么帮助吗?!

标签: phpwindowsdrupalscheduled-tasks

解决方案


php.exe 放错地方了。将其移至其原始位置


推荐阅读