0

I've got a custom plugin where I'm trying to update a global variable. The variable field type is of type Entry

I'm getting the set using getSetByHandle then using getContent on the set. However, when I do a count on the retrieved content, the value is 1. I have about 10 global variables so would've expected a count of 10.

I'm clearly doing something wrong, so a nudge in the right direction would be appreciated.

Update

Here's the code I'm currently using

$global_set = craft()->globals->getSetByHandle('globalVariables');
$selected_poll = craft()->entries->getEntryById($poll_id);
$content = $global_set->getContent();
craft()->globals->saveSet($globalSet);

Update

I've also tried the following, with no luck:

$globalSet = craft()->globals->getSetByHandle('tempSet');
$content = $globalSet->getContent();
$content->currentItem = craft()->entries->getEntryById($itemID);
$globalSet->setContent($content);
craft()->globals->saveContent($globalSet);
  • 1
    Could you edit your question to show the code you are using to grab the Global from the DB, edit it, and then save? We could better assist in a solution that way. – Bryan Redeagle Sep 9 '15 at 15:13
0

Ok figured it out. I was going about it completely the wrong way. As the field in question is an Entry type, I needed to use the Relations Service

$globalSet = craft()->globals->getSetByHandle('[globalSetHAndle]');
$selectedField = craft()->fields->getFieldByHandle('[fieldHandle]');
craft()->relations->saveRelations($selectedField, $globalSet, array($entryId));

Passing null instead of array($entryId) will clear the relation values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy