<p>I don't have much experience using tasks, but I often write plugin methods to move data around. Typically, I just use the EntriesService <a href="https://craftcms.com/classreference/services/EntriesService#saveEntry-detail" rel="nofollow"><code>saveEntry</code></a> or RelationService <a href="https://craftcms.com/classreference/services/RelationsService#saveRelations-detail" rel="nofollow"><code>saveRelation</code></a> as needed.</p>

<p>Here is a sample from a recent data move:</p>

<pre><code>    // Assign category and save entries
    foreach($unassignedEntries as $entry) {

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

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

CC BY-SA 3.0
2016-03-23T05:07:51.833Z
2016-03-23T05:07:51.833Z
Douglas McDonald
<p>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 <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">recommended</a> <code>BaseElementModel::setContentFromPost()</code> method, I have to also account for all kinds of other values to get it to validate and save.</p>

<p>I know you need to specify the IDs (from questions like <a href="https://craftcms.stackexchange.com/questions/12368/updating-a-single-custom-field-within-an-entry">these</a> <a href="https://craftcms.stackexchange.com/questions/11661/programmatically-update-entry-field">two</a>) for any relation fields that are required, which is not ideal, but not a huge issue in this case.</p>

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

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

<plugin-development><content>
Updating just a single content field from a plugin
# Updating just a single content field from a plugin

(Asked by: username_0 on 2016-02-26)

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](https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust) `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](https://craftcms.stackexchange.com/questions/12368/updating-a-single-custom-field-within-an-entry) [two](https://craftcms.stackexchange.com/questions/11661/programmatically-update-entry-field)) 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.

**Tags:** plugin-development, content


---

## 1 answers

### Answer by username_1 on 2016-03-23. Score: 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);
        }
    }


---

