4

I have a Task that is running through a collection of elements, moving some data from one field to another. The problem I am hitting (more and more with situations like this) is that in order to update just one field of an element, using the recommended BaseElementModel::setContentFromPost() method, I have to also account for all kinds of other values to get it to validate and save.

I know you need to specify the IDs (from questions like these two) for any relation fields that are required, which is not ideal, but not a huge issue in this case.

However, on this project I also have the Venti plugin installed, with its Venti_EventFieldType for dates and RRULEs, and I'm having to manually pass through a big array of any existing start/end date information into setContentFromPost() just so the validation for that field will pass, even though I'm doing absolutely nothing with that field.

Is there not a single way to update a single field's value, without already knowing the way data needs to be formatted for any other fields that might be on that ContentModel? Otherwise I don't see how a general-release plugin could be made that can modify existing elements, without knowing about the setContentFromPost()-specific data format of every possible Field Type in advance.

2

I don't have much experience using tasks, but I often write plugin methods to move data around. Typically, I just use the EntriesService saveEntry or RelationService saveRelation as needed.

Here is a sample from a recent data move:

    // Assign category and save entries
    foreach($unassignedEntries as $entry) {

        $success = craft()->relations->saveRelations($field, $entry, array($categoryId));

        if (!$success) {
            LogicPlugin::log('Couldn’t update entry "'.$entry->title.'"', LogLevel::Error);
        }
        else {
            LogicPlugin::log('Successfully updated entry "'.$entry->title.'"', LogLevel::Info);
        }
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.