I have some options set during the install of a transport package, everything works fine ~ on the first install, though if the package is updated and the user tries to change the values [that were retrieved from the current settings] they are not saved. I am assuming it's because I have to set something in he UPGRADE section? But I am not sure what, can someone point me in the right direction?
$modx =& $object->xpdo;
$success = false;
switch ($options[xPDOTransport::PACKAGE_ACTION]) {
// actions on install
case xPDOTransport::ACTION_INSTALL:
$object = $modx->newObject('modSystemSetting');
$object->set('name', 'API Key');
$object->set('key', 'package.api_key');
$object->set('value', $options['api_key']);
$object->set('namespace', 'mypackage');
$object->set('area', 'mypackage');
$object->save();
$success = true;
break;
// actions on upgrade
case xPDOTransport::ACTION_UPGRADE:
$success = true;
break;
// actions on uninstall
case xPDOTransport::ACTION_UNINSTALL:
$success = true;
break;
}
thanks
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
Silly me - it's actually quite easy:
case xPDOTransport::ACTION_UPGRADE:
$object = $modx->getObject('modSystemSetting',array('key' => 'mypackage.api_key'));
$object->set('name', 'mypackage API Key');
$object->set('key', 'mypackage.api_key');
$object->set('value', $options['api_key']);
$object->set('namespace', 'mypackage');
$object->set('area', 'mypackage');
$object->save();
$success = true;
break;
Though I probably only have to set the value, the rest prolly won't change.
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
-
☆ A M B ☆
- 3,141 Posts
I prefer both retrieving and creating to be done in both install and upgrade mode.
In other words, always attempt to get it before creating it even in install. It might be there from an earlier install, and if you try to create a setting with a key that already exists you will get errors in the error log.
...
case xPDOTransport::ACTION_INSTALL:
case xPDOTransport::ACTION_UPGRADE:
$object = $modx->getObject('modSystemSetting',array('key' => 'mypackage.api_key'));
if (!$object) {
$object = $modx->newObject('modSystemSetting');
}
...
Preference, but good practice I guess.
Extremely good point, so if we are getting/setting all these on install OR upgrade, something like:
switch ($options[xPDOTransport::PACKAGE_ACTION]) {
// actions on uninstall
case xPDOTransport::ACTION_UNINSTALL:
$success = true;
break
// actions on install or upgrade
default:
$object = $modx->getObject('modSystemSetting',array('key' => 'mypackage.api_key'));
if (!$object) {
$object = $modx->newObject('modSystemSetting');
}
$object->fromArray(array(
'name' => 'mypackage API Key',
'key' => 'mypackage.api_key',
'value' => $options['api_key'],
'namespace' => 'mypackage',
'area' => 'mypackage',
));
$object->save();
;
}
Or do we really need to split them into upgrade and install [since they will be identical.... and there are 6 or so settings to manage... ]
-thanks
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
-
☆ A M B ☆
- 3,141 Posts
Yeah that'll work.
No need to split it up. As in my example you can also use multiple case's in the switch statement, but the result is pretty much the same..
actually it much prefers to be like this or else it will complain about a duplicate key....
// actions on install or upgrade
case xPDOTransport::ACTION_INSTALL:
case xPDOTransport::ACTION_UPGRADE:
$object = $modx->getObject('modSystemSetting',array('key' => 'mypackage.api_key'));
if (!$object) {
$object = $modx->newObject('modSystemSetting');
$object->set('key', 'mypackage.api_key');
}
$object->set('name' , 'mypackage API Key');
$object->set('namespace' , 'mypackage');
$object->set('area' , 'mypackage');
$object->set('value' , $options['api_key']);
$object->save();
-sean
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
-
☆ A M B ☆
- 3,141 Posts
Could also change your fromArray call to turn off key protection (aka mass assignment protection):
$object->fromArray(array(
'name' => 'mypackage API Key',
'key' => 'mypackage.api_key',
'value' => $options['api_key'],
'namespace' => 'mypackage',
'area' => 'mypackage',
), '', true);
Quote from: markh at Apr 15, 2012, 01:05 PMCould also change your fromArray call to turn off key protection (aka mass assignment protection):
That is much prettier
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525