Magento shell命令致命错误:Class;Mage”;找不到


Magento shell command fatal error: Class "Mage" not found

当我从/shell/目录中运行此命令时:

php -f regularPromos.php

为什么收到此错误

PHP Fatal error: Class 'Mage' not found in /var/www/vhosts/yoohoo/httpdocs/shell/regularPromos.php on line 28

这是regularPromos.php:

<?php
require_once 'abstract.php';
class Mage_Shell_RegularPromos extends Mage_Shell_Abstract
{     
    //Day of week to repeat promotion
    protected $day;
    //ID of promotion
    protected $promoID;
    //Rule process object
    protected $rule;

    public function Mage_Shell_RegularPromos($promoID, $day)
    {
        $this->day = $day;
        $this->promoID = $promoID;
        $this->rule = Mage::getModel('salesrule/rule');
    }

    public function run()
    {
        date_default_timezone_set('America/New_York');
        $nextWeek = date('Y-m-d', strtotime('next '. $this->day));
        $rule = $rule->load($this->promoID);
        $rule->setFromDate($nextWeek)
                ->setToDate($nextWeek)
                ->save();
    }
}
$shell = new Mage_Shell_RegularPromos(7, 'monday');
$shell->run();
?>

根据我能在这个问题上找到的所有SO线程:

  • 我试着在编译器打开/关闭/清除/编译的情况下运行,但出现了相同的错误消息

  • 我已经通过管理面板清除了缓存并手动删除/var/cache/中的所有内容。

  • APC没有显示在phpinfo()中,所以这不应该是一个问题任何一个

我可以很好地运行compiler.php,所以我认为我只是在上面的php中犯了一个错误。

我正在运行Magento 1.7 CE,PHP 5.3.3

如果你在脚本的开头添加这个:

require_once('./app/Mage.php');
Mage::app();

问候

您错误地扩展了基本抽象类。如果你看看抽象构造函数

#File: shell/abstract.php
public function __construct()
{
    if ($this->_includeMage) {
        require_once $this->_getRootPath() . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
        Mage::app($this->_appCode, $this->_appType);
    }
    $this->_applyPhpVariables();
    $this->_parseArgs();
    $this->_construct();
    $this->_validate();
    $this->_showHelp();
}

您可以在这里看到外壳脚本Mage应用程序类中的require。您的类使用旧的PHP语法重新定义了构造函数

public function Mage_Shell_RegularPromos($promoID, $day)
{
    $this->day = $day;
    $this->promoID = $promoID;
    $this->rule = Mage::getModel('salesrule/rule');
}

但是您还没有调用父构造函数。这意味着mage类不是required,其他重要的初始化也不会发生。我会重新定义您的构造函数方法,使其使用更新的__construct语法,并调用父构造函数

public function __construct($promoID, $day)
{
    parent::__construct();
    $this->day = $day;
    $this->promoID = $promoID;
    $this->rule = Mage::getModel('salesrule/rule');
}

我也遇到了同样的问题,但使用了indexer.php等核心shell脚本。我仍然不确定是什么原因导致了这个问题,但它一定与环境有关,因为shell脚本在开发服务器上运行良好,但在生产服务器上出现了问题。

基本上我收到了这个错误:

PHP Fatal error:  Class 'Mage' not found in abstract.php on line 86

这个有问题的代码是:

84. if ($this->_includeMage) {
85.    require_once $this->_getRootPath() . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
86.    Mage::app($this->_appCode, $this->_appType);
87. }

然后我把require_once改为require,poof错误消失了,一切都很好。它似乎也适用于开发服务器。

当发生这种类型的错误时:

致命错误:在/home1/sumit/public_html/index.php 中找不到类"Mage"

在不刷新页面的情况下,将index.php替换为cache_flush,然后点击回车键。