简体   繁体   中英

Joomla 3 - Loading Model from CLI file

I'm trying to load a model class and it's functions inside a CLI script for Joomla 3.6. This is what I have so far:

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

The var_dump always returns false and the final function call always returns this:

Error displaying the error page: Application Instantiation Error: Call to a member function finaliseUpgrade() on boolean

The error is of course because the class hasn't been created and it's really a bool.

So the question remains, how do I pull that model into my php CLI application and use it's functions?

UPDATE

Ok, I've gotten part of the way there, now it return the object but while running finalise it returns an error about Instantiation. Object:

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) {
  }
}

The error is:

Error displaying the error page: Application Instantiation Error: Application Instantiation Error

UPDATE

This is the code that partially works:

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();

The problem was I needed to trick Joomla into thinking we were actually inside the backend admin, something I forgot to include in my initial code starting:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM