Joomla 3 - 从CLI文件加载模型

时间:2016-11-02 13:10:42

标签: php joomla

我正在尝试加载一个模型类,它的功能在Joomla 3.6的CLI脚本中。这就是我到目前为止所做的:

jimport('joomla.application.component.model');
JLoader::import('JoomlaupdateModelDefault', './administrator/components/com_joomlaupdate/models');
$updater = JModelLegacy::getInstance('default');
var_dump($updater);
$updater->finaliseUpgrade();

var_dump总是返回false,最后的函数调用总是返回:

  

显示错误页面时出错:应用程序实例化错误:调用   到布尔值

的成员函数finaliseUpgrade()

错误当然是因为该类尚未创建,而且它真的是一个布尔。

所以问题仍然存在,如何将该模型拉入我的php CLI应用程序并使用它的功能?

更新

好的,我已经在那里获得了一部分,现在它返回了对象但是在运行finalise时它返回了一个关于Instantiation的错误。对象:

object(JoomlaupdateModelDefault)#52 (7) {
  ["__state_set":protected]=>
  NULL
  ["_db":protected]=>
  object(JDatabaseDriverMysqli)#7 (23) {
    ["name"]=>
    string(6) "mysqli"
    ["serverType"]=>
    string(5) "mysql"
    ["connection":protected]=>
    object(mysqli)#11 (19) {
      ["affected_rows"]=>
      int(1)
      ["client_info"]=>
      string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $"
      ["client_version"]=>
      int(50012)
      ["connect_errno"]=>
      int(0)
      ["connect_error"]=>
      NULL
      ["errno"]=>
      int(0)
      ["error"]=>
      string(0) ""
      ["error_list"]=>
      array(0) {
      }
      ["field_count"]=>
      int(14)
      ["host_info"]=>
      string(25) "Localhost via UNIX socket"
      ["info"]=>
      NULL
      ["insert_id"]=>
      int(0)
      ["server_info"]=>
      string(23) "5.7.16-0ubuntu0.16.04.1"
      ["server_version"]=>
      int(50716)
      ["stat"]=>
      string(138) "Uptime: 10319  Threads: 3  Questions: 8381  Slow queries: 0  Opens: 1523  Flush tables: 1  Open tables: 184  Queries per second avg: 0.812"
      ["sqlstate"]=>
      string(5) "00000"
      ["protocol_version"]=>
      int(10)
      ["thread_id"]=>
      int(131)
      ["warning_count"]=>
      int(0)
    }
    ["nameQuote":protected]=>
    string(1) "`"
    ["nullDate":protected]=>
    string(19) "0000-00-00 00:00:00"
    ["_database":"JDatabaseDriver":private]=>
    string(3) "j34"
    ["count":protected]=>
    int(4)
    ["cursor":protected]=>
    NULL
    ["debug":protected]=>
    bool(false)
    ["limit":protected]=>
    int(0)
    ["log":protected]=>
    array(0) {
    }
    ["timings":protected]=>
    array(0) {
    }
    ["callStacks":protected]=>
    array(0) {
    }
    ["offset":protected]=>
    int(0)
    ["options":protected]=>
    array(9) {
      ["driver"]=>
      string(6) "mysqli"
      ["host"]=>
      string(9) "localhost"
      ["user"]=>
      string(4) "***"
      ["password"]=>
      string(4) "***"
      ["database"]=>
      string(3) "***"
      ["prefix"]=>
      string(6) "***_"
      ["select"]=>
      bool(true)
      ["port"]=>
      int(3306)
      ["socket"]=>
      NULL
    }
    ["sql":protected]=>
    object(JDatabaseQueryMysqli)#9 (24) {
      ["offset":protected]=>
      int(0)
      ["limit":protected]=>
      int(0)
      ["db":protected]=>
      *RECURSION*
      ["sql":protected]=>
      NULL
      ["type":protected]=>
      string(6) "select"
      ["element":protected]=>
      NULL
      ["select":protected]=>
      object(JDatabaseQueryElement)#57 (3) {
        ["name":protected]=>
        string(6) "SELECT"
        ["elements":protected]=>
        array(1) {
          [0]=>
          string(1) "*"
        }
        ["glue":protected]=>
        string(1) ","
      }
      ["delete":protected]=>
      NULL
      ["update":protected]=>
      NULL
      ["insert":protected]=>
      NULL
      ["from":protected]=>
      object(JDatabaseQueryElement)#56 (3) {
        ["name":protected]=>
        string(4) "FROM"
        ["elements":protected]=>
        array(1) {
          [0]=>
          string(12) "`#__updates`"
        }
        ["glue":protected]=>
        string(1) ","
      }
      ["join":protected]=>
      NULL
      ["set":protected]=>
      NULL
      ["where":protected]=>
      object(JDatabaseQueryElement)#55 (3) {
        ["name":protected]=>
        string(5) "WHERE"
        ["elements":protected]=>
        array(1) {
          [0]=>
          string(22) "`extension_id` = '700'"
        }
        ["glue":protected]=>
        string(5) " AND "
      }
      ["group":protected]=>
      NULL
      ["having":protected]=>
      NULL
      ["columns":protected]=>
      NULL
      ["values":protected]=>
      NULL
      ["order":protected]=>
      NULL
      ["autoIncrementField":protected]=>
      NULL
      ["call":protected]=>
      NULL
      ["exec":protected]=>
      NULL
      ["union":protected]=>
      NULL
      ["unionAll":protected]=>
      NULL
    }
    ["tablePrefix":protected]=>
    string(6) "oaetn_"
    ["utf":protected]=>
    bool(true)
    ["utf8mb4":protected]=>
    bool(true)
    ["errorNum":protected]=>
    int(0)
    ["errorMsg":protected]=>
    string(0) ""
    ["transactionDepth":protected]=>
    int(0)
    ["disconnectHandlers":protected]=>
    array(0) {
    }
  }
  ["name":protected]=>
  string(7) "default"
  ["option":protected]=>
  string(16) "com_joomlaupdate"
  ["state":protected]=>
  object(JObject)#53 (1) {
    ["_errors":protected]=>
    array(0) {
    }
  }
  ["event_clean_cache":protected]=>
  string(19) "onContentCleanCache"
  ["_errors":protected]=>
  array(0) {
  }
}

错误是:

  

显示错误页面时出错:应用程序实例化错误:   应用程序实例化错误

更新

这是部分有效的代码:

jimport('joomla.application.component.model');
jimport('joomla.application.component.view');
JModelLegacy::addIncludePath('./administrator/components/com_joomlaupdate/models', 'JoomlaupdateModel');
$updater = JModelLegacy::getInstance('Default', 'JoomlaupdateModel');
var_dump($updater);

$updater->finaliseUpgrade();

1 个答案:

答案 0 :(得分:0)

问题是我需要欺骗Joomla以为我们实际上是在后端管理员中,这是我忘记在初始代码中包含的内容:

$_SERVER['HTTP_HOST'] = 'domain.com';
JFactory::getApplication('administrator');