【问题标题】:Is it possible to use an offset when using the Symfony sfPager class?使用 Symfony sfPager 类时是否可以使用偏移量?
【发布时间】:2009-12-05 18:48:46
【问题描述】:

我想忽略分页器项目列表中的前 n 个项目。 IE。它们正在设计中的其他地方使用。

所以我的寻呼机列表需要这样:

Page 1: Items  8 - 17
Page 2: Items 18 - 27
Page 3: Items 28 - 37
...

但是,在条件对象中设置偏移量或限制没有任何作用。我认为它们是由寻呼机本身使用的。

是否可以通过其他方式向寻呼机类添加偏移量?

【问题讨论】:

    标签: symfony1 pagination


    【解决方案1】:

    好的,我通过修改 sfPropelPager.class.php 并将其放入我命名为 atPropelPagerOffset.class.php 的新类文件中解决了这个问题

    除了需要一个额外的参数$offset之外,它的工作方式完全相同

    所以文件的顶部看起来像:

      protected
        $criteria               = null,
        $peer_method_name       = 'doSelect',
        $peer_count_method_name = 'doCount',
        $offset                 = 0;
    
      public function __construct($class, $maxPerPage = 10, $offset = 0)
      {
        parent::__construct($class, $maxPerPage);
    
        $this->setCriteria(new Criteria());
        $this->tableName = constant($class.'Peer::TABLE_NAME');
        $this->offset = $offset;
      }
    

    然后我在第 50 行附近做了这个微小的改变

      $c->setOffset($offset+$this->offset);
    

    工作愉快!

    【讨论】:

      【解决方案2】:

      更简单的解决方案是自定义选择方法:

      $pager->setPeerMethod('doSelectCustom');
      

      然后把你的逻辑放到模型Peer Class中:

      public static function doSelectCustom($c)
      {
         $c2 = clone $c;
         $offset = $c2->getOffset();
         $limit = $c2->getLimit();
         $someCustomVar = someClass::someMethod();
         if ($offset == 0) // we are in the first page 
         {
            $c2->setLimit($limit - $someCustomVar);
            $c2->add(self::SOMECOLUMN, false);
         } else $c2->setOffset($offset - $someCustomVar);
         return self::doSelectRS($c2); // or doSelect if you wanna retrieve objects
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-10
        • 2016-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-02
        • 1970-01-01
        相关资源
        最近更新 更多