PHP code example of xj / yii2-xunsearch

1. Go to this page and download the library: Download xj/yii2-xunsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xj / yii2-xunsearch example snippets


return [
    'components' => [
        'xunsearch' => [
            'class' => 'xj\\xunsearch\\Connection',
            //Put Xunsearch ini to $configDirectory
            'configDirectory' => '@common/config/xunsearch',
        ],
    ],
];

class Demo extends \xj\xunsearch\ActiveRecord {

    public static function primaryKey() {
        return ['pid'];
    }

    public function rules() {
        return [
            [['pid', 'subject', 'message'], '

$model = new Demo();
$model->setAttributes([
    'pid' => 1,
    'subject' => 'haha',
    'message' => 'hehe',
]);
$model->save();

//where syntax
$models = Demo::find()->where([
            'wild', 'key1', '-key2', // key1 -key2
            'wild', 'key1', 'key2', 'key3', // key1 key2 key3
            'pid' => [5, 6, 7], // (pid:5) OR (pid:6) OR (pid:7)
            'pid' => 5, // pid:5
            'and', 'key1', 'key2', 'key3', // (key1) AND (key2) AND (key3)
            'or', '5', '6', '7', // (5) OR (6) OR (7)
            'and', '啊', ['or', 'pid:30', 'pid:31'] // (啊) AND ((pid:30) OR (pid:31))
        ])->all();
var_dump($models);

//asArray
$models = Demo::find()->where([
            'wild', 'key1', '-key2', // key1 -key2
        ])->asArray()->all();

$model = Demo::findByPk(1);
$model->subject = 'mod subject';
$model->save();

$model = Demo::findByPk(1);
$model->delete();

$count = Demo::find()->where([
            'wild', 'key1',
        ])->count();

$query = Demo::find();
$dataProvider = new \yii\data\ActiveDataProvider([
    'query' => $query,
]);
$models = $dataProvider->getModels();
var_dump($models);