We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28215
    • 4,149 Posts
    BobRay,

    I had no problems doing the scenario you described. I created 2 categories, one that packages chunks, the other snippets, then attached 2 resolvers to the snippet category, and in the resolver changed the snippet description based upon input from setup-options. It worked fine.

    So as to your problem, I can’t really help you unless you show me your entire build script. I can’t figure out if you have problems elsewhere, say in your resolver, with only partial code from the build script.

    Thanks for pluggin away at this.

    P.S. Here’s my code to my build script:

    <?php
    /**
     * @package test
     * @subpackage build
     */
    $mtime = microtime();
    $mtime = explode(" ", $mtime);
    $mtime = $mtime[1] + $mtime[0];
    $tstart = $mtime;
    set_time_limit(0);
    
    $root = dirname(dirname(__FILE__)) . '/';
    $sources= array (
        'root' => $root,
        'build' => $root . '_build/',
        'lexicon' => $root . '_build/lexicon/',
        'resolvers' => $root . '_build/resolvers/',
        'docs' => $root . 'test/docs/',
    );
    
    /* override with your own defines here (see build.config.sample.php) */
    require_once $sources['build'].'build.config.php';
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    
    $modx= new modX();
    $modx->initialize('mgr');
    echo '<pre>';
    $modx->setLogLevel(MODX_LOG_LEVEL_INFO);
    $modx->setLogTarget('ECHO');
    
    /* load packagebuilder and transport package */
    $modx->loadClass('transport.modPackageBuilder','',false, true);
    $builder = new modPackageBuilder($modx);
    $builder->createPackage('test','0.1','alpha');
    $builder->registerNamespace('test',false);
    
    
    /* add in snippets */
    $attr = array(
        XPDO_TRANSPORT_UNIQUE_KEY => 'category',
        XPDO_TRANSPORT_PRESERVE_KEYS => false,
        XPDO_TRANSPORT_UPDATE_OBJECT => true,
        XPDO_TRANSPORT_RELATED_OBJECTS => true,
        XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array (
            'modSnippet' => array (
                XPDO_TRANSPORT_PRESERVE_KEYS => false,
                XPDO_TRANSPORT_UPDATE_OBJECT => true,
                XPDO_TRANSPORT_UNIQUE_KEY => 'name',
            )
        )
    );
    $category= $modx->newObject('modCategory');
    $category->set('id',1);
    $category->set('category','TestCompSnippets');
    
    $snippets = array();
    for ($i=1;$i<4;$i++) {
        $c= $modx->newObject('modSnippet');
        $c->set('id',$i);
        $c->set('name', 'TestSnippet'.$i);
        $c->set('description', 'Test '.$i);
        $c->set('snippet', file_get_contents($sources['root'] . 'test/elements/snippet'.$i.'.class.php'));
    
        $snippets[] = $c;
    }
    $category->addMany($snippets,'modSnippet');
    
    $vehicle = $builder->createVehicle($category,$attr);
    $vehicle->resolve('file',array(
        'source' => $sources['root'] . 'test',
        'target' => "return MODX_ASSETS_PATH . 'components/';",
    ));
    $vehicle->resolve('php',array(
        'source' => $sources['resolvers'] . 'test.resolver.php',
    ));
    $builder->putVehicle($vehicle);
    
    /* create chunks */
    $attr = array(
        XPDO_TRANSPORT_UNIQUE_KEY => 'category',
        XPDO_TRANSPORT_PRESERVE_KEYS => false,
        XPDO_TRANSPORT_UPDATE_OBJECT => true,
        XPDO_TRANSPORT_RELATED_OBJECTS => true,
        XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array (
            'modChunk' => array (
                XPDO_TRANSPORT_PRESERVE_KEYS => false,
                XPDO_TRANSPORT_UPDATE_OBJECT => true,
                XPDO_TRANSPORT_UNIQUE_KEY => 'name',
            )
        )
    );
    $category= $modx->newObject('modCategory');
    $category->set('id',2);
    $category->set('category','TestCompChunks');
    $chunks = array();
    for ($i=1;$i<2;$i++) {
        $c = $modx->newObject('modChunk');
        $c->set('id',$i);
        $c->set('name', 'TestChunk'.$i);
        $c->set('description', 'Test Chunk '.$i);
        $c->set('snippet', file_get_contents($sources['root'] . 'test/elements/chunk'.$i.'.tpl'));
        $chunks[] = $c;
    }
    $category->addMany($chunks);
    $vehicle = $builder->createVehicle($category,$attr);
    $builder->putVehicle($vehicle);
    
    /* build in lexicon */
    $builder->buildLexicon($sources['lexicon']);
    
    /* now pack in the license file, readme and setup options */
    $builder->setPackageAttributes(array(
        'license' => file_get_contents($sources['docs'] . 'license.txt'),
        'readme' => file_get_contents($sources['docs'] . 'readme.txt'),
        'setup-options' => file_get_contents($sources['build'] . 'setup.options.tpl'),
    ));
    
    /* pack */
    $builder->pack();
    
    $mtime= microtime();
    $mtime= explode(" ", $mtime);
    $mtime= $mtime[1] + $mtime[0];
    $tend= $mtime;
    $totalTime= ($tend - $tstart);
    $totalTime= sprintf("%2.4f s", $totalTime);
    
    $modx->log(MODX_LOG_LEVEL_INFO,"\n<br />Package Built.<br />\nExecution time: {$totalTime}\n");
    exit();
    


    And my resolver:

    <?php
    $success= false;
    switch ($options[XPDO_TRANSPORT_PACKAGE_ACTION]) {
        case XPDO_TRANSPORT_ACTION_INSTALL:
        case XPDO_TRANSPORT_ACTION_UPGRADE:
            $snippet = $object->xpdo->getObject('modSnippet',array('name' => 'TestSnippet1'));
            if ($snippet != null) {
                $snippet->set('description',$options['snippetDescription']);
                $snippet->save();
            } else {
                $object->xpdo->log(XPDO_LOG_LEVEL_ERROR,'TestSnippet1 could not be found, so the description could not be changed.');
            }
            $success= true;
            break;
        case XPDO_TRANSPORT_ACTION_UNINSTALL:
            $success= true;
            break;
    }
    return $success;


    And my setup.options.tpl:
    <label for="snippetDescription">Snippet 1 Description:</label>
    <input type="text" name="snippetDescription" id="snippetDescription" width="300" value="The first test snippet." />
    
      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • 3749
      • 24,544 Posts
      That’s not exactly what I did (I didn’t describe it clearly enough).

      I created the snippets first.
      * I attached the resolver that alters a snippet properties to that snippet’s vehicle.

      I added each snippet and chunk (as it’s created) to an array to use in setting the categories.
      Then I created the categories.

      That resolver executed, but had no effect.

      I was able to solve my problem (at about 4am) by moving the resolver down and attaching it to the last thing I created (a resource).
      I’m sure attaching it to the category as you did would have worked also.

      It seems, though, that it should have worked as I originally had it. The resolver script *does* find the snippet object and successfully sets it’s properties and saves it, so the snippet must have been created by that point. Adding that snippet to the category with addMany() later seems to undo the changes made in the script (in fact the properties set in the script are not set at all when you look at the snippet properties in the Manager).

      I’m almost set to go. The only hangup now is that the newly created resources have no template. Is there something I have to do to give them the default template? I assumed it would be automatic. (On Jira as MODX-525). Ah ... just saw your comment on that. I assumed that the process would be the same as creating a resource in the Manager, where they get the default template.

      That settled, poor old René is the only remaining problem I know about. wink

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28215
        • 4,149 Posts
        Quote from: BobRay at Nov 24, 2008, 06:43 PM

        I’m almost set to go. The only hangup now is that the newly created resources have no template. Is there something I have to do to give them the default template? I assumed it would be automatic. (On Jira as MODX-525). Ah ... just saw your comment on that. I assumed that the process would be the same as creating a resource in the Manager, where they get the default template.
        Nope, unfortunately, there’s no way for that to be ’automated’ currently, and we’d rather leave that up to the developers on how to handle those circumstances.


        That settled, poor old René is the only remaining problem I know about. wink

        Yeah, still no clue. I say it’s just Rene for now. tongue
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 3749
          • 24,544 Posts
          I’m baffled again (what else is new). embarrassed

          The message in the console says that the default template is 1 but the template of the resource is still set to zero with this code in the resolver:

          $default_template = $object->xpdo->config['default_template'];
          
          $obj = $object->xpdo->getObject('modSnippet',array('name'=>'SPFResponse'));
                      if (!$obj) {
                            $object->xpdo->log(XPDO_LOG_LEVEL_ERROR,'Could not get SPFResponse object');
                      } else {
                           $object->xpdo->log(XPDO_LOG_LEVEL_INFO,'Default template: ' . $default_template);
          
                          $obj->set('template',$default_template);  /* give it the default template */
                          if ($obj->save() == false ) {
                               $object->xpdo->log(XPDO_LOG_LEVEL_ERROR,'Could not save SPForm properties');
                          }
                      }
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28215
            • 4,149 Posts
            Quote from: BobRay at Nov 24, 2008, 07:34 PM

            I’m baffled again (what else is new).  embarrassed

            The message in the console says that the default template is 1 but the template of the resource is still set to zero with this code in the resolver:
            $obj = $object->xpdo->getObject(’modSnippet’,array(’name’=>’SPFResponse’));
            $obj->set(’template’,$default_template);  /* give it the default template */


            It’s because you’re setting the ’template’ field for the Snippet (which doesn’t exist) rather than the Resource. You should be getting the resource here instead.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 3749
              • 24,544 Posts
              Quote from: splittingred at Nov 24, 2008, 07:55 PM

              Quote from: BobRay at Nov 24, 2008, 07:34 PM

              I’m baffled again (what else is new). embarrassed

              The message in the console says that the default template is 1 but the template of the resource is still set to zero with this code in the resolver:
              $obj = $object->xpdo->getObject(’modSnippet’,array(’name’=>’SPFResponse’));
              $obj->set(’template’,$default_template); /* give it the default template */


              It’s because you’re setting the ’template’ field for the Snippet (which doesn’t exist) rather than the Resource. You should be getting the resource here instead.

              Duh. embarrassed I figured that out and was hoping I could get back here to delete my message before you replied. wink I definitely need more sleep. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting