-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfonySphinxPager.class.php
More file actions
110 lines (92 loc) · 2.57 KB
/
SymfonySphinxPager.class.php
File metadata and controls
110 lines (92 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Class that extends the symfony pager to work with sphinx query results
*
* @author Martín Ernesto Barreyro, Marc St Raymond
*/
class SymfonySphinxPager extends sfPager
{
/**
* Constructor
* @param object $class
* @param integer $maxPerPage
* @param sfSphinxClient $sphinx
*/
public function __construct($class, HeardaboutSphinxClient $sphinx, $maxPerPage = 15)
{
parent::__construct($class, $maxPerPage);
$this->sphinx = $sphinx;
$this->query = Doctrine::getTable($this->getClass())->createQuery();
}
/**
* A function to be called after parameters have been set
*/
public function init()
{
$hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
$maxRecordLimit = $this->getMaxRecordLimit();
$results = $this->sphinx->querySphinx();
if ($results === false)
{
return;
}
$count = $this->sphinx->getTotalFound();
//Set "number" of results
$this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);
if (($this->getPage() == 0 || $this->getMaxPerPage() == 0))
{
$this->setLastPage(0);
}
else
{
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
if ($hasMaxRecordLimit)
{
$maxRecordLimit = $maxRecordLimit - $offset;
if ($maxRecordLimit > $this->getMaxPerPage())
{
$limit = $this->getMaxPerPage();
} else
{
$limit = $maxRecordLimit;
}
}
else
{
$limit = $this->getMaxPerPage();
}
$this->sphinx->setLimit("$offset, $limit");
}
}
/**
* Return results for given page
* @return Doctrine_Collection
*/
public function getResults()
{
$sphinx_results = $this->sphinx->querySphinx();
//First we need to get the Ids
$ids = array();
$collection = array();
if (count($sphinx_results) > 0)
{
//Reformat the array of results so that we can use it in the query
foreach ($sphinx_results as $value)
{
$ids[] = $value['id'];
}
// Then we retrieve the objects correspoding to the found Ids
// Obviously update this query to fetch from the appropriate table...using 'Object' as a placeholder
$q = Doctrine::getTable('Object')
->createQuery('o')
->whereIn('o.id', $ids)
->orderBy('FIELD(o.id, ' . implode(',', $ids) . ')');
$collection = $q->execute();
}
return $collection;
}
/* FORCED TO DEFINE THIS METHOD BECAUSE IT IS ABSTRACT IN THE PARENT CLASS (sfPager) */
public function retrieveObject($offset)
{}
}