您需要使用的函数order()
接受两个常量之一:
- ORDER_ASCENDING
- ORDER_DESCENDING
在 php 中,所有类常量都是公开可见的。要访问常量,可以使用以下代码:CouchbaseViewQuery::ORDER_ASCENDING
或CouchbaseViewQuery::ORDER_DESCENDING
.
下面是一个使用Couchbase Server 附带的Beer-sample数据的代码示例。
<?php
// Connect to Couchbase Server
$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$bucket = $cluster->openBucket('beer-sample');
$query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_ASCENDING);
$results = $bucket->query($query);
foreach($results['rows'] as $row) {
var_dump($row['key']);
}
echo "Reversing the order\n";
$query = CouchbaseViewQuery::from('beer', 'by_location')->skip(6)->limit(2)->reduce(false)->order(CouchbaseViewQuery::ORDER_DESCENDING);
$results = $bucket->query($query);
foreach($results['rows'] as $row) {
var_dump($row['key']);
}
以下是上述代码的输出:
array(3) {
[0]=>
string(9) "Australia"
[1]=>
string(15) "New South Wales"
[2]=>
string(6) "Sydney"
}
array(3) {
[0]=>
string(9) "Australia"
[1]=>
string(15) "New South Wales"
[2]=>
string(6) "Sydney"
}
Reversing the order
array(3) {
[0]=>
string(13) "United States"
[1]=>
string(7) "Wyoming"
[2]=>
string(8) "Cheyenne"
}
array(3) {
[0]=>
string(13) "United States"
[1]=>
string(7) "Wyoming"
[2]=>
string(6) "Casper"
}