<p>You are currently performing a <code>SELECT</code>instead of an <code>UPDATE</code> so no data will be updated.</p>

<p>I'm guessing that <code>entryId</code> is the primary key of the record you are trying to update? If so, I would suggest you find the record to make sure it exists, update its attributes (status) and then save it.</p>

<pre class="lang-php prettyprint-override"><code>// PluginController
public function actionDeny()
{
    $this-&gt;requirePostRequest();

    $status  = PluginStatus::Denied;
    $entryId = craft()-&gt;request-&gt;getRequiredPost('entryId');

    if (craft()-&gt;pluginName-&gt;updateStatus($entryId, $status)
    {
        craft()-&gt;userSession-&gt;setError(Craft::t('Unable to deny the entry.'));
    }
    else
    {
        craft()-&gt;userSession-&gt;setNotice(Craft::t('The entry was denied.'));
    }
}

// PluginService
public function updateStatus($id, $status)
{
    $record = PluginRecord::model()-&gt;findById($id);

    if ($record)
    {
        $record-&gt;setAttribute('status', $status);

        return $record-&gt;save();
    }
    else
    {
        throw new Exception(Craft:t('No record was found with id {id}', array('id' =&gt; $id));
    }
}
</code></pre>

<p>Unless I'm missing something, that should be about all you need.</p>

<p><a href="http://twitter.com/selvinortiz" rel="noreferrer">@selvinortiz</a></p>

CC BY-SA 3.0
2014-07-16T05:53:43.073Z
2014-07-16T05:53:43.073Z
Selvin Ortiz
<p>I have a custom element type with its own table 'pluginname_entries'. I am trying to find a way to "dummy proof" changing the plugins status. So I came up, aka stole from the Events element type, with the idea of clicking a button that triggers a controller action - similar to the following example to delete my element types entry:</p>

<p><strong>actionDelete()</strong></p>

<pre><code>/**
 * Deletes an entry.
 */
public function actionDelete()
{
    $this-&gt;requirePostRequest();

    $entryId = craft()-&gt;request-&gt;getRequiredPost('entryId');

    if (craft()-&gt;elements-&gt;deleteElementById($entryId))
    {
        craft()-&gt;userSession-&gt;setNotice(Craft::t('Entry deleted.'));
        $this-&gt;redirectToPostedUrl();
    }
    else
    {
        craft()-&gt;userSession-&gt;setError(Craft::t('Couldn’t delete entry.'));
    }
}
</code></pre>

<p>I love the concept or a quick way to delete or update the entry, more specifically the entry custom status, with a simple click of a button. So I came up with this:</p>

<p><strong>actionDeny()</strong></p>

<pre><code>/**
 * Changes an entry to denied.
 */
public function actionDeny()
{
    $this-&gt;requirePostRequest();

    $status = PluginStatus::Denied;

    $entryId = craft()-&gt;request-&gt;getRequiredPost('entryId');

    $query = craft()-&gt;db-&gt;createCommand()
        -&gt;select('id, status')
        -&gt;from('entries')
        -&gt;where(array(
            'id' =&gt; $entryId
        ))-&gt;queryRow();

    if ($query == null) {
        craft()-&gt;userSession-&gt;setError(Craft::t('Unable to deny the entry.'));
    }
    else {
        craft()-&gt;userSession-&gt;setNotice(Craft::t('The entry was denied.'));
    }

}
</code></pre>

<p>That works as intended, notifying the user but of course doesn't actually update the <code>status</code> column to the new value, is there a better way to handle this? Maybe more of a "Craft" way?</p>

<plugin-development><controller><queries>
How do I update a column value in my custom plugin with a simple controller?
# How do I update a column value in my custom plugin with a simple controller?

(Asked by: username_0 on 2014-07-16)

I have a custom element type with its own table 'pluginname_entries'. I am trying to find a way to "dummy proof" changing the plugins status. So I came up, aka stole from the Events element type, with the idea of clicking a button that triggers a controller action - similar to the following example to delete my element types entry:
actionDelete()
/**
 * Deletes an entry.
 */
public function actionDelete()
{
    $this->requirePostRequest();

    $entryId = craft()->request->getRequiredPost('entryId');

    if (craft()->elements->deleteElementById($entryId))
    {
        craft()->userSession->setNotice(Craft::t('Entry deleted.'));
        $this->redirectToPostedUrl();
    }
    else
    {
        craft()->userSession->setError(Craft::t('Couldn’t delete entry.'));
    }
}

I love the concept or a quick way to delete or update the entry, more specifically the entry custom status, with a simple click of a button. So I came up with this:
actionDeny()
/**
 * Changes an entry to denied.
 */
public function actionDeny()
{
    $this->requirePostRequest();

    $status = PluginStatus::Denied;

    $entryId = craft()->request->getRequiredPost('entryId');

    $query = craft()->db->createCommand()
        ->select('id, status')
        ->from('entries')
        ->where(array(
            'id' => $entryId
        ))->queryRow();

    if ($query == null) {
        craft()->userSession->setError(Craft::t('Unable to deny the entry.'));
    }
    else {
        craft()->userSession->setNotice(Craft::t('The entry was denied.'));
    }

}

That works as intended, notifying the user but of course doesn't actually update the status column to the new value, is there a better way to handle this? Maybe more of a "Craft" way?

**Tags:** plugin-development, controller, queries

---

## 1 answers

### Answer by username_1 on 2014-07-16. Score: 5 | Accepted answer

You are currently performing a SELECTinstead of an UPDATE so no data will be updated.
I'm guessing that entryId is the primary key of the record you are trying to update? If so, I would suggest you find the record to make sure it exists, update its attributes (status) and then save it.
// PluginController
public function actionDeny()
{
    $this->requirePostRequest();

    $status  = PluginStatus::Denied;
    $entryId = craft()->request->getRequiredPost('entryId');

    if (craft()->pluginName->updateStatus($entryId, $status)
    {
        craft()->userSession->setError(Craft::t('Unable to deny the entry.'));
    }
    else
    {
        craft()->userSession->setNotice(Craft::t('The entry was denied.'));
    }
}

// PluginService
public function updateStatus($id, $status)
{
    $record = PluginRecord::model()->findById($id);

    if ($record)
    {
        $record->setAttribute('status', $status);

        return $record->save();
    }
    else
    {
        throw new Exception(Craft:t('No record was found with id {id}', array('id' => $id));
    }
}

Unless I'm missing something, that should be about all you need.
@selvinortiz


---

