Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Model_Logic_Sphinx
- */
- class Model_Logic_Sphinx {
- const INDEX_ARTICLES = 'articles';
- protected $_oSphinx;
- protected $_aConfig;
- /**
- *
- * @var Model_Logic_Sphinx
- */
- static private $_oInstance;
- /**
- * Liczba wyników
- * @var int
- */
- protected $_iTotalFound = 0;
- /**
- *
- * @return Model_Logic_Sphinx
- */
- public static function getInstance() {
- if (self::$_oInstance == null) {
- self::$_oInstance = new self();
- }
- return self::$_oInstance;
- }
- /**
- * Construct method
- */
- public function __construct() {
- $this->_aConfig =Model_Resource_Options::get('sphinx');
- $this->_oSphinx = new Common_SphinxClient();
- $this->_oSphinx->SetServer($this->_aConfig['host'], $this->_aConfig['port']);
- $this->_oSphinx->SetMatchMode($this->_aConfig['mode']);
- $this->_oSphinx->SetArrayResult(true);
- $this->_oSphinx->SetLimits(0, 1000);
- }
- /**
- *
- * @param string $sQuery
- * @param array $aFilters Filtering values $aFilter = array('{col}' => array('wartosci'))
- * @param array $aParams array with limit and offset
- * @return mixed
- */
- public function query($sQuery, $aFilters = array(), array $aParams = array()) {
- $this->_oSphinx->ResetFilters();
- if (isset($aParams['limit']) && isset($aParams['offset'])) {
- $this->_oSphinx->SetLimits((int) $aParams['offset'], (int) $aParams['limit']);
- }
- if (!empty($aFilters)) {
- foreach ($aFilters as $aAttrib => $aFilter) {
- $this->_oSphinx->SetFilter($aAttrib, $aFilter);
- }
- }
- $sIndex = $aParams['index'];
- $aResult = $this->_oSphinx->Query($sQuery, $sIndex);
- if ($aResult === false) {
- throw new Zend_Exception("Sphinx query failed: " . $this->_oSphinx->GetLastError());
- } else {
- if ($this->_oSphinx->GetLastWarning()) {
- throw new Zend_Exception("Sphinx warning: " . $this->_oSphinx->GetLastWarning());
- }
- }
- $this->_iTotalFound = $aResult['total_found'];
- $aIds = array();
- if (!empty($aResult['matches'])) {
- foreach ($aResult['matches'] as $aMatch) {
- $aIds[] = $aMatch['id'];
- }
- }
- return $aIds;
- }
- public function getTotalFound() {
- return $this->_iTotalFound;
- }
- }
Add Comment
Please, Sign In to add comment