<p>I've got the beginnings of a simple plugin to track views of an article page, just an entry with custom fields including 'dataViews' used below.</p>

<p>Within Twig:</p>

<pre><code>{% do craft.demoPlugin.incrementView(entry) %}
</code></pre>

<p>Variable above set with:</p>

<pre><code>&lt;?php
namespace Craft;

class DemoPluginVariable
{
    /**
     * Increment count
     *
     * @param EntryModel $entry
     */
    public function incrementView(EntryModel $entry)
    {
        craft()-&gt;demoPlugin-&gt;incrementView($entry);
    }
}
</code></pre>

<p>Here's the service code which picks up the</p>

<pre><code>&lt;?php
namespace Craft;


class DemoPluginService extends BaseApplicationComponent
{
    /**
     * Increment view count
     *
     * @param EntryModel $entry
     */
    public function incrementView(EntryModel $entry)
    {
        $entry-&gt;setContentFromPost([
            'dataViews' =&gt; 1234,
        ]);


        $success = craft()-&gt;entries-&gt;saveEntry($entry);
        Craft::dd($success);
    }
}
</code></pre>

<p>The above fails when passed an entry, with $success reporting false. Logging the error this is due to other fields on the entry being required and not passed through.</p>

<p>Could anyone point me in the direction for updating just one field within a record?</p>

CC BY-SA 3.0
2015-11-12T22:36:22.847Z
Instead of `setContentFromPost`, try using `$entry->dataViews = 1234;`.
Brad Bell
CC BY-SA 3.0
2015-11-13T08:22:53.983Z
Must admit I've tried that as well Brad, drawing a bit of blank to be honest.
Chris Southam
CC BY-SA 3.0
2015-11-13T12:17:31.553Z
Have you see this? https://github.com/putyourlightson/craft-entry-count
Oli
<plugin-development><entry><entrymodel>
Updating a single custom field within an Entry
# Updating a single custom field within an Entry

(Asked by: username_0 on 2015-11-12)

I've got the beginnings of a simple plugin to track views of an article page, just an entry with custom fields including 'dataViews' used below.
Within Twig:
{% do craft.demoPlugin.incrementView(entry) %}

Variable above set with:
demoPlugin->incrementView($entry);
    }
}

Here's the service code which picks up the
setContentFromPost([
            'dataViews' => 1234,
        ]);


        $success = craft()->entries->saveEntry($entry);
        Craft::dd($success);
    }
}

The above fails when passed an entry, with $success reporting false. Logging the error this is due to other fields on the entry being required and not passed through.
Could anyone point me in the direction for updating just one field within a record?

**Tags:** plugin-development, entry, entrymodel
> **Comments:**
> **username_1**: Instead of `setContentFromPost`, try using `$entry->dataViews = 1234;`.  
> **username_0**: Must admit I've tried that as well Brad, drawing a bit of blank to be honest.  
> **username_2**: Have you see this? https://github.com/putyourlightson/craft-entry-count  

---

## 0 answers

