复古/yii2-search

在Yii2框架的Active Record模型中进行搜索

v2.0.0 2018-02-09 22:50 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:43:46 UTC


README

在Yii2框架的Active Record模型中进行搜索

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version Latest Unstable Version

这是一个用于在Yii2框架的Active Record模型中进行搜索的组件。

文档位于docs/guide/README.md

安装

安装包

安装此扩展的首选方式是通过composer

运行以下命令

$ composer require vintage/yii2-search

或者在您的composer.json文件的require部分添加以下内容

"vintage/yii2-search": "~2.0"

使用方法

在Active Record模型中实现\vintage\search\interfaces\SearchInterface接口

/**
 * Article search model.
 * 
 * @property integer $id
 * @property string $title
 * @property string $short_description
 * @property string $content
 */
class ArticleSearch extends ActiveRecord implements \vintage\search\interfaces\SearchInterface
{
    /**
     * @inheritdoc
     */
    public function getSearchTitle()
    {
    
        return $this->title;
    }

    /**
     * @inheritdoc
     */
    public function getSearchDescription()
    {
        return $this->short_description;
    }

    /**
     * @inheritdoc
     */
    public function getSearchUrl()
    {
       return Url::toRoute(['/news/default/index', 'id' => $this->id]);
    }

   /**
    * @inheritdoc
    */
    public function getSearchFields()
    {
        return [
            'title',
            'short_description',
            'content',
        ];
    }
}

使用查询调用搜索组件的方法

/* @var \vintage\search\models\SearchResult[] $result */
$result = Yii::$app->get('searcher')->search('some query here');

此方法返回一个包含\vintage\search\models\SearchResult对象的数组。

测试

您可以使用composer命令运行测试

$ composer test

或者使用以下命令

$ codecept build && codecept run

贡献

有关贡献的信息,请阅读CONTRIBUTING.md

许可证

License

本项目根据BSD-3-Clause许可证条款发布许可证