sfSphinxDoctrinePager.class.php 3.43 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
 * sfSphinx Doctrine pager class
 * @package sfSphinxPlugin
 * @author  Kamil Rojewski <krojew@o2.pl>
 */

class sfSphinxDoctrinePager extends sfPager
{
  protected
    $keyword   = null,
    $sphinx    = null,
    $pk_column = 'id',
    $query     = null;

  /**
   * Constructor
   * @param object         $class
   * @param integer        $maxPerPage
   * @param sfSphinxClient $sphinx
   */
  public function __construct($class, $maxPerPage = 10, sfSphinxClient $sphinx)
  {
    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();

    $res = $this->sphinx->getRes();
    if ($res === false)
    {
      return;
    }

    $count = $res['total_found'];

    $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->SetLimits($offset, $limit);
    }
  }

  /**
   * Get the query for the pager
   * @return Doctrine_Query $query
   */
  public function getQuery()
  {
    return $this->query;
  }

  /**
   * Set query object for the pager
   * @param  Doctrine_Query $query
   */
  public function setQuery(Doctrine_Query $query)
  {
    $this->query = $query;
  }

  /**
   * Get Pk column name
   * @return string
   */
  public function getPkColumn()
  {
    return $this->pk_column;
  }

  /**
   * Set Pk column name
   * @param  string $column
   * @return void
   */
  public function setPkColumn($column)
  {
    $this->pk_column = $column;
  }

  /**
   * Retrieve an object of a certain model with offset
   * used internally by getCurrent()
   * @param  integer $offset
   * @return object
   */
  protected function retrieveObject($offset)
  {
    $this->sphinx->SetLimits($offset - 1, 1); // We only need one object

    $res = $this->sphinx->getRes();
    if ($res['total_found'] == 0)
    {
      return null;
    }

    $id = $match['id'][0];

    $query = clone $this->getQuery();

    return $query
      ->where($this->getPkColumn() . ' = ?', $id)
      ->fetchOne();
  }

  /**
   * Return results for given page
   * @return Doctrine_Collection
   */
  public function getResults()
  {
    $res = $this->sphinx->getRes();
    if ($res['total_found'] == 0)
    {
      return array();
    }
    // First we need to get the Ids
    $ids = array();
    foreach ($res['matches'] as $match)
    {
      $ids[] = $match['id'];
    }
    // Then we retrieve the objects correspoding to the found Ids
    $query = clone $this->getQuery();

    return $query
      ->whereIn($this->getPkColumn(), $ids)
      ->orderBy('FIELD(' . $this->getPkColumn() . ', ' . implode(',', $ids) . ')')
      ->execute();
  }
}