Search code examples
phpmodxmodx-revolution

Cant create new Resource. Call to a member function newObject() on null


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

Solution

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

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