避免使用者忘記自己在第幾頁

edited 十月 2013 in CakePHP
有使用者反應,在分頁的資料中瀏覽,經常在往返間忘記自己在第幾頁,或是每次要點回自己看的那一頁都有些麻煩,所以我在 AppController 加入了下面這個方法:
function paginate($object = null, $scope = array(), $whitelist = array()) {
        if (is_string($object)) {
            $assoc = null;

            if (strpos($object, '.') !== false) {
                list($object, $assoc) = explode('.', $object);
            }

            if ($assoc && isset($this->{$object}->{$assoc})) {
                $object = $this->{$object}->{$assoc};
            } elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) {
                $object = $this->{$this->modelClass}->{$assoc};
            } elseif (isset($this->{$object})) {
                $object = $this->{$object};
            } elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) {
                $object = $this->{$this->modelClass}->{$object};
            }
        } elseif (empty($object) || $object == null) {
            if (isset($this->{$this->modelClass})) {
                $object = $this->{$this->modelClass};
            } else {
                $className = null;
                $name = $this->uses[0];
                if (strpos($this->uses[0], '.') !== false) {
                    list($name, $className) = explode('.', $this->uses[0]);
                }
                if ($className) {
                    $object = $this->{$className};
                } else {
                    $object = $this->{$name};
                }
            }
        }
        if (!is_object($object)) {
            trigger_error(sprintf(__('Controller::paginate() - can\'t find model %1$s in controller %2$sController', true), $object, $this->name), E_USER_WARNING);
            return array();
        }
        $alias = 'paginate.' . $this->params['controller'] . '.' . $this->params['action'] . '.' . $object->alias;
        $sessionVars = $this->Session->read($alias);
        if(!empty($this->passedArgs)) {
            if(!empty($sessionVars)) {
                $this->passedArgs = array_merge($sessionVars, $this->passedArgs);
            }
            $this->Session->write($alias, $this->passedArgs);
        } else if(!empty($sessionVars)) {
            $this->passedArgs = $sessionVars;
        }
        return parent::paginate($object, $scope, $whitelist);
    }

這可以暫時解決這個問題,只是不知道有沒有比較好的處理方式?

相關討論(英文):
http://groups.google.com/group/cake-php/browse_thread/thread/74d2f261e47346d5

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=3554
Sign In or Register to comment.