Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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