I need (if possible) to have a package create a document, get that document’s id number, create a system setting, and use the id number to set the system setting’s value. (Actually, I need to do this all twice).
I can imagine (sort of) how the whole sequence could be done in a script, but is there an easier way?
if ($id = $object->get('id')) {
$setting = $object->xpdo->newObject('modSystemSetting');
$setting->set('key','keynamehere');
$setting->set('namespace','nshere');
$setting->set... // continue with other values
$setting->set('value',$id);
$setting->save();
}
Quote from: BobRay at Oct 03, 2008, 03:10 PM
I need (if possible) to have a package create a document, get that document’s id number, create a system setting, and use the id number to set the system setting’s value. (Actually, I need to do this all twice).
I can imagine (sort of) how the whole sequence could be done in a script, but is there an easier way?
Best way is through a resolver. Put the Resource in a vehicle, add a resolver that then does something like this:
if ($id = $object->get('id')) { $setting = $object->xpdo->newObject('modSystemSetting'); $setting->set('key','keynamehere'); $setting->set('namespace','nshere'); $setting->set... // continue with other values $setting->set('value',$id); $setting->save(); }
My jaw literally dropped open when I saw how easily this can be done.
Is there an example somewhere of how to put a Resource in a vehicle one the fly while setting its fields (e.g. title, longtitle, hide from menu, and content)?
Here’s a tougher one. I’d like to set the parent of a Resource I’m creating to the ID of the one just created.
$success= false;
if ($id = $object->get('id')) {
switch (XPDO_TRANSPORT_PACKAGE_ACTION) {
case XPDO_TRANSPORT_ACTION_INSTALL:
$child = $object->xpdo->newObject('modResource');
$child->set('pagetitle','Child');
...
$child->set('parent',$object->get('id');
$child->save();
$success= true;
break;
case XPDO_TRANSPORT_ACTION_UPDATE:
$child = $object->xpdo->getObject('modResource',array(
'pagetitle' => 'Child',
'parent' => $object->get('id'),
));
if ($child == null) {
$child = $object->xpdo->newObject('modResource');
}
// if i wanted to change resource settings on update
// ...or make sure the child resource exists if it was deleted
// ...or reset them if modified...
$child->set('longtitle','New longtitle for update');
$child->save();
$success= true;
break;
case XPDO_TRANSPORT_ACTION_UNINSTALL:
// not doing anything b/c we dont want a new object when uninstalling
$success= true;
break;
}
return $success;
PS: It might be time to create a "package building" forum area -- maybe in the team area for now but moved public on beta?
This may also be a case where XPDO_TRANSPORT_RELATED_OBJECTS could be used. If you create the parent object and the child and use the getOne()/addOne()/getMany()/addMany() functions to attach it to the parent, you can include both in one vehicle and it will automatically resolve the foreign key relations on install. For example, here is a complex set of related objects packed and installed from a single Vehicle:
Here’s a tougher one. I’d like to set the parent of a Resource I’m creating to the ID of the one just created.
<?php
require_once dirname(__FILE__) . '/build.config.php';
require_once (MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx= new modX();
$modx->initialize('mgr');
$cacheManager= $modx->getCacheManager();
$modx->setLogLevel(MODX_LOG_LEVEL_WARN);
$modx->setLogTarget('FILE');
// load the MODx transport package builder
$modx->loadClass('transport.modPackageBuilder','',false, true);
$builder = new modPackageBuilder($modx);
// create the transport with name, version string, and optional release
$name= 'test';
$version= '0.0.0';
$release= '';
$namespace= 'test';
$builder->create($name, $version, $release);
$collection = $modx->getCollectionGraph('modResource', '{"modTemplate":{"modTemplateVarTemplate":{}},"modTemplateVarResource":{}}', "modResource.`context_key` IN ('web','test')");
$attributes = array (
XPDO_TRANSPORT_PRESERVE_KEYS => true,
XPDO_TRANSPORT_UPDATE_OBJECT => true,
XPDO_TRANSPORT_RELATED_OBJECTS => true,
XPDO_TRANSPORT_RELATED_OBJECT_ATTRIBUTES => array(
'modTemplate' => array(
XPDO_TRANSPORT_PRESERVE_KEYS => false,
XPDO_TRANSPORT_UPDATE_OBJECT => true,
XPDO_TRANSPORT_UNIQUE_KEY => 'templatename',
),
'modTemplateVar' => array(
XPDO_TRANSPORT_PRESERVE_KEYS => false,
XPDO_TRANSPORT_UPDATE_OBJECT => true,
XPDO_TRANSPORT_UNIQUE_KEY => 'name',
),
'modTemplateVarTemplate' => array(
XPDO_TRANSPORT_PRESERVE_KEYS => false,
XPDO_TRANSPORT_UPDATE_OBJECT => true,
),
'modTemplateVarResource' => array(
XPDO_TRANSPORT_PRESERVE_KEYS => false,
XPDO_TRANSPORT_UPDATE_OBJECT => true,
),
)
);
foreach ($collection as $c) {
if ($c->modTemplate->modTemplateVarTemplate) {
foreach ($c->modTemplate->modTemplateVarTemplate as $k => $tvt) {
$tvt->getOne('modTemplateVar');
}
}
$vehicle = $builder->createVehicle($c, $attributes);
$builder->putVehicle($vehicle);
}
unset ($collection, $c, $vehicle, $attributes);
$builder->pack();
?>