douji8549 2009-10-10 14:23
浏览 32
已采纳

修订系统 - 帮助改进我的代码

A section of a site I am building needs some form of revision system and so decided to keep it simple and go with one similar to Stack Overflow.

I quickly created the following which works although seems a little messy. I know I can use beforeSave and afterSave but I have no idea how I could implement it.

I know I could probably move more to the model but are they any other ways to improve it?

Revisions controller:

class RevisionsController extends AppController {
    var $name = "Revisions";
    var $helpers = array('Diff');

    /**
     * View a list of existing revisions.
     * Displays the changes between revisions with the
     * Diff helper.
     */
    function view($seriesId = null) {
        if ((!$seriesId && empty($this->data))) {
            $this->Session->setFlash('That Series does not exist', true, array('class' =>
                'error'));
            $this->redirect('/');
        }

        $revisions = $this->Revision->find('all', array('conditions' => array('Revision.series_id' =>
            $seriesId), 'order' => 'revision desc', 'contain' => false));
        $this->set('revisions', $revisions);
    }

    /**
     * Create a new revision by editing the most recent one.
     * This seems very messy.
     */
    function edit($seriesId = null) {
        if ((!$seriesId && empty($this->data))) {
            $this->Session->setFlash('That Series does not exist', true, array('class' =>
                'error'));
            $this->redirect('/');
        }

        if (empty($this->data)) {
            $latest = $this->Revision->find('first', array('conditions' => array('is_latest' =>
                1, 'series_id' => $seriesId)));
            $this->data['Revision']['description'] = $latest['Revision']['description'];
        } else {
            $this->data['Revision']['revision'] = $this->Revision->getNext($seriesId);
            $this->data['Revision']['series_id'] = $seriesId;
            $this->Revision->create();
            if ($this->Revision->save($this->data)) {
                $this->Revision->setLatest($this->Revision->id, $seriesId);

                $this->Session->setFlash('The edit has been saved.', true, array('class' =>
                    'success'));
                $this->redirect(array('controller' => 'series', 'action' => 'view', $seriesId));
            }
        }

        $this->set('seriesId', $seriesId);
    }


    /**
     * Creates a new revision using data from a previous one.
     * Currently clones the previous revision rather than
     * just linking it. May need to be changed.
     */
    function rollback($seriesId = null, $revision = null) {
        if (!$seriesId || !$revision) {
            $this->redirect('/');
        }

        $this->data = $this->Revision->find('first', array('conditions' => array('Revision.series_id' =>
            $seriesId, 'Revision.revision' => $revision), 'contain' => false));

        $this->data['Revision']['revision'] = $this->Revision->getNext($seriesId);
        $this->data['Revision']['is_rollback'] = 1;
        $this->data['Revision']['rollback_rev'] = $revision;
        unset($this->data['Revision']['id']);
        $this->Revision->create();
        if ($this->Revision->save($this->data)) {
            $this->Revision->setLatest($this->Revision->id, $seriesId);

            $this->Session->setFlash('The rollback has been saved.', true, array('class' =>
                'success'));
            $this->redirect(array('controller' => 'series', 'action' => 'view', $seriesId));
        }
    }

}

Revision model:

class Revision extends AppModel {

    var $name = 'Revision';

    var $belongsTo = array('Series' => array('counterCache' => true));
    var $actsAs = array('Containable');

    function getCurrent($seriesId = null) {
        if (!$seriesId)
            return false;

        $series = $this->Series->find('first', array('conditions' => array('Series.id' =>
            $seriesId), 'contain' => false));
        return $series['Series']['revision_count'];
    }

    function getNext($seriesId = null) {
        if (!$seriesId)
            return false;

        $revision = $this->getCurrent($seriesId);
        return $revision + 1;
    }

    function setLatest($id, $seriesId = null) {
        $this->updateAll(array('Revision.is_latest' => 0), array('Revision.series_id' =>
            $seriesId));

        $this->id = $id;
        $this->saveField('is_latest', 1);
    }

}
  • 写回答

1条回答 默认 最新

  • dongyoucha0645 2009-10-10 23:37
    关注

    Unless this is something you want to apply to every model, this would be better suited as a Behavior. It would be a case of moving that revision specific logic out of AppModel and into a Behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度