0

I need to create resources programmaticaly, but errour occur. I have one parent Uki(4), I am able to create resource manually. When I run function, that uses $data array, to create resource - it gives null on $modx->newObject('modResouce');

I've tried to use $modx->runProcessor('resource/create', $createresource); - it also gives on null error.

require_once '/opt/lampp/htdocs/modx/config.core.php';
require_once( MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx = new modX();
$modx->initialize('web');
//Подгрузка сервисов модекса

define('MODX_API_MODE', true);
$modx->getService('error','error.modError');
$modx->lexicon->load('minishop2:default');
$modx->lexicon->load('minishop2:manager');
$miniShop2 = $modx->getService('minishop2','miniShop2',
$modx->getOption('minishop2.core_path',null,
$modx->getOption('core_path').'components/minishop2/').'model/minishop2/', array());


function productAddItemBD($data)
{     


$doc = $modx->newObject('modResouce');    //81 line =====
    $doc->set('parent', '0');
    $doc->set('pagetitle', $data['pagetitle']);
    $doc->set('alias', $data['alias']);
    $doc->save();



    $modifications = $modx->call('msopModification', 'saveProductModification', array(&$modx, $doc->id, $data['modprices']));
    $response = 
            $modx->runProcessor('gallery/upload', array('id' => $doc->id, 
                'name' => $data['title'], 'file' => $data['img']), 
                array('processors_path' => MODX_CORE_PATH . 'components/minishop2/processors/mgr/'));
    if ($response->isError()) {
        throw new Exception(get_error_string($response,' Site: create modification or gallery item'));
    }
$item_created++;
  return true;

}

Gives error:

    Fatal error: Uncaught Error: Call to a member function newObject() on null in 
    /opt/lampp/htdocs/modx/core/cache/includes/elements/modsnippet/2.include.cache.php:81 Stack trace: #0   
      /opt/lampp/htdocs/modx/core/cache/includes/elements/modsnippet/2.include.cache.php(56): productAddItemBD(Array) #1 
/opt/lampp/htdocs/modx/core/cache/includes/elements/modsnippet/2.include.cache.php(41): goThroughArrayOFPRODUCTS(Array) #2 
    /opt/lampp/htdocs/modx/core/model/modx/modscript.class.php(76): include('/opt/lampp/htdo...') #3     
    /opt/lampp/htdocs/modx/core/model/modx/modparser.class.php(537): modScript->process(NULL) #4 
    /opt/lampp/htdocs/modx/core/components/pdotools/model/pdotools/pdoparser.class.php(273): modParser->processTag(Array, false) #5 
    /opt/lampp/htdocs/modx/core/model/modx/modparser.class.php(251): pdoParser->processTag(Array, false) #6 
    /opt/lampp/htdocs/modx/core/components/pdotools/model/pdotools/pdoparser.class.php(65): modParser->processElementTags('[[]]', '<!doctype html>...', false, false, '[[', ']]', Array, 8) #7 /opt/lampp in 
    /opt/lampp/htdocs/modx/core/cache/includes/elements/modsnippet/2.include.cache.php on line 81
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
Kolia
  • 17
  • 7
  • $modx is outside the function scope. –  Aug 04 '19 at 22:03
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) –  Aug 04 '19 at 22:04

2 Answers2

1

The variable "$modx" does not exist within the function.

Declare the variable within the function, or use "global $modx;"

jjuniob
  • 71
  • 4
  • Uncaught Error: Call to a member function set() on null (same position) – Kolia Aug 04 '19 at 22:16
  • Checks if "$doc" object has been created: "var_dump($doc);", or use "$doc->fromArray(array( arguments ))"; – jjuniob Aug 04 '19 at 22:23
  • $doc gives NULL – Kolia Aug 04 '19 at 22:29
  • Tried runProcessor - gives error Call to a member function initialize() on null – Kolia Aug 04 '19 at 22:40
  • "newObject" method is returning null, debugs checking why it is not loading the class. Try to enable modx log_level https://code.osu.edu/ucom/modx/blob/a65eab18ae1161d7f41f234db97cbff98c3fa788/core/xpdo/xpdo.class.php#L779 – jjuniob Aug 04 '19 at 22:46
0

$modx->newObject('modResouce'); Here the "r" in "modResource" is missing.

For running processes your usergroup must have correct access. I'll attach a screenshot modx admin panel

csabinho
  • 1,579
  • 1
  • 18
  • 28