We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    OK I’m trying to work out what is needed to package a sample site with templates, tv’s and resources to start with and to later add in required snippets too.

    so I have one template and one resource with one tv.

    So far I have managed to work out how to package the template and the resource and how to associate the 2 together.

    what I can’t work out is how to create the tv and associate it to the template. this is what i have so far

    /*Some variable setup above here snipped for ease */
    
    require_once dirname(__FILE__) . '/build.config.php';
    
    require_once MODX_CORE_PATH . '/model/modx/modx.class.php';
    
    $modx = new modx();
    $modx->initialize('mgr');
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
    
    $modx->loadclass('transport.modPackageBuilder', '', false, true);
    $builder = new modPackageBuilder($modx);
    $builder->createPackage('splashSunrise', '0.1', 'alpha1');
    
    $modx->log(modX::LOG_LEVEL_INFO, 'Templates');
    $mainTemplate = $modx->newObject('modTemplate');
    $mainTemplate->set('id', 1);
    $mainTemplate->set('templatename', 'BaseTemplate');
    $mainTemplate->set('content', file_get_contents($sources['templates'] . 'BaseTemplate.html'));
    
    $modx->log(modX::LOG_LEVEL_INFO, 'Resources');
    $homeResource = $modx->newObject('modResource');
    $homeResource->set('id', 1);
    $homeResource->set('pagetitle', 'Home');
    $homeResource->set('published', 1);
    $homeResource->set('content', file_get_contents($sources['resources'] . 'home.html'));
    
    $mainTemplate->addMany($homeResource);
    
    $modx->log(modX::LOG_LEVEL_INFO, 'TemplateVars');
    $tagTemplateVar = $modx->newObject('modTemplateVar');
    $tagTemplateVar->set('id', 1);
    $tagTemplateVar->set('name', 'tags');
    
    $mainTemplate->addMany($tagTemplateVar);
    
    $attr = array(
        xPDOTransport::UNIQUE_KEY => 'templatename',
        xPDOTransport::PRESERVE_KEYS => true,
        xPDOTransport::UPDATE_OBJECT => true,
        xPDOTransport::RELATED_OBJECTS => true,
        xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array(
            xPDOTransport::PRESERVE_KEYS => true,
            xPDOTransport::UPDATE_OBJECT => true,
            xPDOTransport::UNIQUE_KEY => 'pagetitle'
        ),
        xPDOTransport::RELATED_OBJECT_ATTRIBUTES => array(
            xPDOTransport::PRESERVE_KEYS => true,
            xPDOTransport::UPDATE_OBJECT => true,
            xPDOTransport::UNIQUE_KEY => 'id'
        )
    );
    
    $vehicle = $builder->createVehicle($mainTemplate, $attr);
    
    $modx->log(modX::LOG_LEVEL_INFO, 'Adding file resolvers to category...');
    $vehicle->resolve('file', array(
        'source' => $sources['source_assets'] . '/templates',
        'target' => "return MODX_ASSETS_PATH . '/';",
    ));
    
    $builder->putVehicle($vehicle);
    $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, "\nPackage Built. \nExecution time : {$totalTime}\n");
    exit()
    


    This runs and this is what i get out
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    Created new transport package with signature: splashsunrise-0.1-alpha1
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    Templates
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    Resources
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    TemplateVars
    [2011-05-24 13:20:03] (ERROR @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    No foreign key definition for parentClass: modTemplate using relation alias: modTemplateVar
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    Adding file resolvers to category...
    [2011-05-24 13:20:03] (INFO @ /modx/splashSurprise/sourceFiles/trunk/_build/build.transport.php)

    Package Built.
    Execution time : 0.2527 s

    This creates the template it creates the resources but nothing happens with the tv the main error that seems to be linked to this is

    No foreign key definition for parentClass: modTemplate using relation alias: modTemplateVar

    Having looked at the schema is see that the template has a modTemplateVarTemplate instead but can’t figure out how to use this to link the template and the tv.

    Any help would be gratefully appreciated.
      • 37743
      • 49 Posts
      BUMP - Old post i know but pretty much exactly my same question -

      How do modTemplate and modTemplateVar relate to each other in xpdo... how does one assign a TV to a template object?
        • 3749
        • 24,544 Posts
        Usually it's easiest to do that in a script resolver. Something like this (but with some sanity checks and error messages):

        <?php
        $modx =& $object->xpdo;
        
        $template = $modx->getObject('modTemplate', array('templatename' => 'YourTemplateName'));
        
        $tv = $modx->getObject('modTemplateVar', array('name'=>'YourTVName'));
        
        /* create templateVarTemplate object and save */
        
        $tvt = $modx->newObject('modTemplateVarTemplate');
        $tvt->set('templateid', $template->get('id'));
        $tvt->set('tmplvarid', $tv->get('id'));
        $tvt->save();
        


        You might look at the MyComponent (getMyComponent) package, which has examples of almost anything you might want to do in a Transport Package: http://bobsguides.com/mycomponent-tutorial.html


        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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