S
S
Sergey2015-04-09 18:09:07
Yii
Sergey, 2015-04-09 18:09:07

How to connect sphinx in yii2 and work with it?

I connect sphinx as a component according to this instruction: https://www.zagirov.name/yii-cookbook-2/ (toward the end of the link there is information about sphinx)
In the config I have the following:

'sphinx' => [
            'class' => 'system.db.CDbConnection',
            'connectionString' => 'mysql:host=127.0.0.1;port=9306',
        ],

In the controller for verification I write the code:
$sSql = 'SELECT id FROM posts WHERE MATCH('.Yii::$app->sphinx->quoteValue($term).')';
$ids = Yii::$app->sphinx->createCommand($sSql)->queryAll();
var_dump($ids); die();

I see an error:
Class system.db.CDbConnection does not exist

Those. does yii2 not have this class? And what then to use?
Or are there other ways to work with sphinx in yii2? Which?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Max, 2015-04-09
@butteff

No, these are examples for the first version of the framework
For yii2, there is https://github.com/yiisoft/yii2-sphinx

I
Igor, 2015-06-24
@mulat

A couple of examples of my Yii2 code from

/**
  * $inCategory
  * @var boolean  false - get posts from alien categories, true - from this category
  */

$postIds = $query
      ->select('id')
      ->from(Yii::$app->getModule('blog')->getParam('sphinxRtIndex'))
      ->match(new Expression(':match',
          [
              'match' => implode(' | ', $search)
          ]
      ))
      ->andWhere('id != :id', [':id' => $this->postModel->id])
      ->andWhere('category_id' . ($this->inCategory? '=' : '!=') . ':category_id', 
             [
                 ':category_id' => $this->postModel->category_id
             ]
      )
      ->limit($this->limit)
      ->all();

from RtSphinxBehavior behavior ( on github ) :
protected function replace() {
    $params = [];

    $sql = \Yii::$app->sphinx->getQueryBuilder()
        ->replace(
            $this->rtIndex,
            $this->getColumns(),
            $params
        );
    return \Yii::$app->sphinx->createCommand($sql, $params)->execute();
}
/**
  * $this->getColumns()  возвращает массив вида:
  * ['title' => 'John Doe', 'text' => 'I knew John well...']
  *
  * $this->rtIndex - содержит значение имени Realtime индекса
 */

D
Dmitry Baibukhtin, 2015-04-09
@PiloTeZ

www.yiiframework.com/doc-2.0/ext-sphinx-index.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question