Answer the question
In order to leave comments, you need to log in
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',
],
$sSql = 'SELECT id FROM posts WHERE MATCH('.Yii::$app->sphinx->quoteValue($term).')';
$ids = Yii::$app->sphinx->createCommand($sSql)->queryAll();
var_dump($ids); die();
Class system.db.CDbConnection does not exist
Answer the question
In order to leave comments, you need to log in
No, these are examples for the first version of the framework
For yii2, there is https://github.com/yiisoft/yii2-sphinx
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();
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 индекса
*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question