阻止更新特定属性

时间:2017-12-14 15:42:04

标签: php codeigniter

我有一个包含列id,collection,shelfmark,sort_order的文档表。

通过更新文档,即使表单中没有sort_order输入字段,sort_order也会设置为0.

我试过

 unset($aData['sort_order']);

作为表单中的最后一个语句但在提交sort_order属性后设置为0。

我的表格 - 片段:

   <form class="form" id="edit-form" method="POST" action="cms/documents/update/<?= $sDocumentId ?>">
     <tr>
          <td><?= form_label('Display Shelfmark:&nbsp;*', 'shelfmark') ?></td>
          <td><?php $aAttributes = array('name'          => 'shelfmark',
                                         'id'            => 'shelfmark',
                                         'value'         => set_value('shelfmark', '', FALSE),
                                         'placeholder'   => 'Example: Cod. Sang. 70'
                                         );
                      $sError = '';
                      if (form_error('shelfmark') != '') {
                        $aAttributes['class'] = 'm form-error';
                        $sError = '<span class="help-inline help-error">' . form_error('shelfmark') . '</span>';
                      }
                      echo form_input($aAttributes) . $sError;
               ?>
            </td>
        </tr>

  <div id="saveButtons">
    <a href="cms/documents/cancelUpdate/<?= $sDocumentId ?>" class="btn">Cancel</a>
    <button type="submit" class="btn btn-success" id="save-document"><i class="icon-ok-sign icon-white"></i> Save &amp; Finish</button>
    <a href="#" id="save-and-continue" class="btn btn-success"><i class="icon-pencil icon-white"></i> Save &amp; Continue Editing</a>
  </div>
</form>  

如何阻止更新数据库表中的sort_order属性?

---更新2:

在我的文档模型中:

  public function updateDocument($sDocumentId, $aDocumentData) {
    $this->filterDataArray('documents_revisions', $aDocumentData);
    // before updating, check if latest revision has identical content (except updated_at and user id)
    if ($this->Revisions_model->revisionIsIdentical($aDocumentData, $this->Revisions_model->getLatestRevision($sDocumentId))) {
      return true;
    }
    $aDocumentData['document_id'] = $sDocumentId;
    $aDocumentData['updated_at'] = date('Y-m-d H:i:s');
    $aDocumentData['ion_auth_user_id'] = $this->ion_auth->user()->row()->id;
    return $this->Revisions_model->createRevision($aDocumentData);
  }

在我的修订模型中:

public function createRevision($aRecordToSave) {
    // get latest revision for the record we are trying to save (there should always be at least one revision, except when adding a new record)
    $oLatestRevision = $this->getLatestRevision($aRecordToSave[$this->sForeignKey]);
    // check if the latest revision is identical to the one we are trying to save - only save if they're not identical
    if ($this->revisionIsIdentical($aRecordToSave, $oLatestRevision)) {
      return true; // don't create a revision if the same record data already exists in revisions table
    }
    else { // latest revision is not identical to the record we are trying to save
      // start a database transaction
      $this->db->trans_begin();

      $this->db->where($this->sForeignKey, $aRecordToSave[$this->sForeignKey]);
      $this->db->update($this->sRevisionsTable, array('latest' => 0));

      // check if there are already <maxNumberOfRevisions> and overwrite oldest revision if necessary
      $aRecordToSave['latest'] = 1;
      if ($this->maxNumberOfRevisionsReached($aRecordToSave[$this->sForeignKey])) { // using foreignKey because record ID is unset already
        $this->db->where('updated_at', $this->getOldestRevisionTime($aRecordToSave[$this->sForeignKey]));
        $this->db->where($this->sForeignKey, $aRecordToSave[$this->sForeignKey]);
        $this->db->update($this->sRevisionsTable, $aRecordToSave);
      }
      else { // insert a new revision
        $this->db->insert($this->sRevisionsTable, $aRecordToSave);
      }

      if($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return false;
      }
      else {
        $this->db->trans_commit();
        return true;
      }
    }
  }

是否有CodeIgniter方法可以排除&#39; sort_order&#39;来自$ aRecordToSave?

1 个答案:

答案 0 :(得分:0)

感谢TigerTiger的评论,我通过将sort_order设置为最新修订版本来解决了这个问题:

$aRecordToSave['sort_order'] = $oLatestRevision->sort_order;