- 1楼
其实这是上游写的一个Feature...不过,对于随时会更改的导航贴,这样做显然不是十分妥当。
这论坛的问题已经搞定了,就借上游坛的图加以说明了:
可以看到,置顶已读后会自己掉下去。
我在上游坛找了找,发现了一个解决方案:
然后我就照着这个改了代码。但事实上,这样做会使得第一页加载更多时无限加载置顶。像这样:
我就很头疼了。
不过,读一下这一段代码,不难发现Toby在注释里说明了下面这一段是检测已读状态的:
// If we are viewing a specific tag, then pin all stickied
// discussions to the top no matter what.
$gambits = $search->getActiveGambits();
if ($count = count($gambits)) {
if ($count === 1 && $gambits[0] instanceof TagGambit) {
if (! is_array($query->orders)) {
$query->orders = [];
}
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']);
}
return;
}
// Otherwise, if we are viewing "all discussions", only pin stickied
// discussions to the top if they are unread. To do this in a
// performant way we create another query which will select all
// stickied discussions, marry them into the main query, and then
// reorder the unread ones up to the top.
$sticky = clone $query;
$sticky->where('is_sticky', true);
$sticky->orders = null;
$query->union($sticky);
$read = $query->newQuery()
->selectRaw(1)
->from('discussion_user as sticky')
->whereColumn('sticky.discussion_id', 'id')
->where('sticky.user_id', '=', $search->getActor()->id)
->whereColumn('sticky.last_read_post_number', '>=', 'last_post_number');
// Add the bindings manually (rather than as the second
// argument in orderByRaw) for now due to a bug in Laravel which
// would add the bindings in the wrong order.
$query->orderByRaw('is_sticky and not exists ('.$read->toSql().') and last_posted_at > ? desc')
->addBinding(array_merge($read->getBindings(), [$search->getActor()->read_time ?: 0]), 'union');
$query->unionOrders = array_merge($query->unionOrders, $query->orders);
$query->unionLimit = $query->limit;
$query->unionOffset = $query->offset;
$query->limit = $sticky->limit = $query->offset + $query->limit;
$query->offset = $sticky->offset = null;
}
}
}
于是我就从75行开始删,同时照着上面的方法囊括前面判定列表顺序的if一起照前人方法替换成了这样:
// If we are viewing a specific tag, then pin all stickied
// discussions to the top no matter what.
$gambits = $search->getActiveGambits();
if ($count = count($gambits)) {
if ($count === 1 && $gambits[0] instanceof TagGambit) {
if (! is_array($query->orders)) {
$query->orders = [];
}
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']);
}
return;
}
// Otherwise, if we are viewing "all discussions", only pin stickied
// discussions to the top if they are unread. To do this in a
// performant way we create another query which will select all
// stickied discussions, marry them into the main query, and then
// reorder the unread ones up to the top.
$sticky = clone $query;
$sticky->where('is_sticky', true);
$sticky->orders = null;
if (! is_array($query->orders)) {
$query->orders = [];
}
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']);
}
}
}
然后就完美地解决了这个问题,虽然我一点也不会PHP,对于原理我也没搞懂