对于极限(20),它只运行DB的前20个值


with limit(20) it runs only the first 20 values of DB

在这几行代码中,它只选择前20个,但我希望第一次选择前20,然后选择其他和其他,依此类推:'(我该如何解决这个问题?

function something_cron()
  $query = db_select('watchdog', 'wa')
    ->extend('PagerDefault')
    ->orderBy('wid')
    ->fields('wa', array('variables', 'type', 'severity',
  'message', 'wid', 'timestamp'))
    ->limit(20);
  $result = $query->execute();
  // Loop through each item and add to $row.
  foreach ($result as $row) {
    someother();
  }

我也想避免这个Pager默认,但我不能。。

首先,如果不显示寻呼机,则不需要PagerDefault。你可以使用orderBy和没有它的范围。

如果我理解你的帖子,每次运行cron时,你都想要看门狗表的前20行。如果是这种情况,我建议跟踪您从表中获得的最后一个看门狗id(wid),并将其用于后续的每次运行。类似于:

$wid = variable_get('my_last_wid', 0);
$result = db_select('watchdog')
  ->fields('watchdog', array('variables', 'type', 'severity', 'message', 'wid', 'timestamp'))
  ->condition('wid', $wid, '>')
  ->orderBy('wid')
  ->range(0, 20)
  ->execute();
foreach ($result as $watchdog) {
  // do whatever you need to do
}
if (!empty($watchdog)) {
  variable_set('my_last_wid', $watchdog->wid);
}

第一次运行时试试这个:

$query = db_select('watchdog', 'wa')
->extend('PagerDefault')
->orderBy('wid')
->fields('wa', array('variables', 'type', 'severity','message', 'wid', 'timestamp'))
->range(0,20);
$result = $query->execute();

这是第二个

$query = db_select('watchdog', 'wa')
->extend('PagerDefault')
->orderBy('wid')
->fields('wa', array('variables', 'type', 'severity','message', 'wid', 'timestamp'))
->range(20,20);
$result = $query->execute();

只需不断增加范围函数的第一个参数即可。

"偏移量"的作用是忽略前x行。如果你有100行,并且你想要最后35行,你可以做"->范围(65,35);"

编辑:以下是range()的工作原理示例。

http://unska.com/drupal_range.png