diff --git a/ActiveQuery.php b/ActiveQuery.php index a3ee867..4ebefe5 100644 --- a/ActiveQuery.php +++ b/ActiveQuery.php @@ -11,13 +11,6 @@ use yii\db\QueryTrait; /** - * ActiveQuery represents a query associated with an Active Record class. - * - * An ActiveQuery can be a normal query or be used in a relational context. - * - * ActiveQuery instances are usually created by [[ActiveRecord::find()]]. - * Relational queries are created by [[ActiveRecord::hasOne()]] and [[ActiveRecord::hasMany()]]. - * * Normal Query * ------------ * @@ -26,45 +19,15 @@ * - [[one()]]: returns a single record populated with the first row of data. * - [[all()]]: returns all records based on the query results. * - [[count()]]: returns the number of records. - * - [[sum()]]: returns the sum over the specified column. - * - [[average()]]: returns the average over the specified column. - * - [[min()]]: returns the min over the specified column. - * - [[max()]]: returns the max over the specified column. - * - [[scalar()]]: returns the value of the first column in the first row of the query result. - * - [[exists()]]: returns a value indicating whether the query result has data or not. * * You can use query methods, such as [[where()]], [[limit()]] and [[orderBy()]] to customize the query options. - * - * ActiveQuery also provides the following additional query options: - * - * - [[with()]]: list of relations that this query should be performed with. - * - [[indexBy()]]: the name of the column by which the query result should be indexed. + * * - [[asArray()]]: whether to return each record as an array. * - * These options can be configured using methods of the same name. For example: - * * ```php - * $customers = Customer::find()->with('orders')->asArray()->all(); + * $demos = Demo::find()->asArray()->all(); * ``` - * - * Relational query - * ---------------- - * - * In relational context ActiveQuery represents a relation between two Active Record classes. - * - * Relational ActiveQuery instances are usually created by calling [[ActiveRecord::hasOne()]] and - * [[ActiveRecord::hasMany()]]. An Active Record class declares a relation by defining - * a getter method which calls one of the above methods and returns the created ActiveQuery object. - * - * A relation is specified by [[link]] which represents the association between columns - * of different tables; and the multiplicity of the relation is indicated by [[multiple]]. - * - * If a relation involves a pivot table, it may be specified by [[via()]]. - * This methods may only be called in a relational context. Same is true for [[inverseOf()]], which - * marks a relation as inverse of another relation. - * - * @author Carsten Brandt - * @since 2.0 + * @author xjflyttp */ class ActiveQuery extends Component implements ActiveQueryInterface { @@ -85,11 +48,19 @@ class ActiveQuery extends Component implements ActiveQueryInterface { 'NOT' => 'buildNotCondition', 'AND' => 'buildAndCondition', 'OR' => 'buildAndCondition', - 'IN' => 'buildInCondition', - 'NOT IN' => 'buildInCondition', 'WILD' => 'buildWildCondition', ]; + /** + * Search Query String Cache + * @var string + */ public $query; + /** + * enable fuzzy query + * @var bool + * @see http://www.xunsearch.com/doc/php/api/XSSearch + */ + public $fuzzy = false; /** * Constructor. @@ -114,10 +85,15 @@ public function init() { private function setCondition(XSSearch $search) { $params = []; - $search->setLimit($this->limit, $this->offset); +// $search->setLimit($this->limit, $this->offset); + $this->buildLimit($this->limit, $this->offset); $this->buildOrderBy($this->orderBy); $this->query = $query = $this->buildWhere($this->where, $params); $search->setQuery($query); + //fuzzy query + if ($this->fuzzy) { + $search->setFuzzy(); + } } /** @@ -158,8 +134,10 @@ public function buildHashCondition($condition, &$params) { $parts = []; foreach ($condition as $column => $value) { if (is_array($value)) { - // IN condition - $parts[] = $this->buildInCondition('IN', [$column, $value], $params); + foreach ($value as $v) { + $parts[] = "$column:$v"; + } + return count($parts) === 1 ? $parts[0] : '(' . implode(') OR (', $parts) . ')'; } else { if ($value !== null) { $parts[] = "$column:$value"; diff --git a/ActiveRecord.php b/ActiveRecord.php index 403d44b..1770530 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -10,27 +10,8 @@ use yii\helpers\StringHelper; /** - * ActiveRecord is the base class for classes representing relational data in terms of objects. - * - * This class implements the ActiveRecord pattern for the [redis](http://redis.io/) key-value store. - * - * For defining a record a subclass should at least implement the [[attributes()]] method to define - * attributes. A primary key can be defined via [[primaryKey()]] which defaults to `id` if not specified. - * - * The following is an example model called `Customer`: - * - * ```php - * class Customer extends \yii\redis\ActiveRecord - * { - * public function attributes() - * { - * return ['id', 'name', 'address', 'registration_date']; - * } - * } - * ``` - * - * @author Carsten Brandt - * @since 2.0 + * Xunsearch AR + * @author xjflyttp */ class ActiveRecord extends BaseActiveRecord { @@ -56,6 +37,18 @@ public static function getDb() { public static function find() { return Yii::createObject(ActiveQuery::className(), [get_called_class()]); } + + /** + * Find Model By PK + * @param int|string $id + * @return Database + */ + public static function findByPk($id) { + $find = static::find(); + $pk = static::primaryKey(); + $pk = $pk[0]; + return $find->where([$pk => $id])->one(); + } /** * Returns the primary key name(s) for this AR class. diff --git a/Connection.php b/Connection.php index 76ed8d8..b3ff4a6 100644 --- a/Connection.php +++ b/Connection.php @@ -10,19 +10,8 @@ use yii\db\Exception; /** - -'xunsearch' => [ - 'class' => 'xj\\xunsearch\\Connection', - 'configDirectory' => '@common/config/xunsearch', -], - * - * - * @property string $driverName Name of the DB driver. This property is read-only. - * @property boolean $isActive Whether the DB connection is established. This property is read-only. - * @property LuaScriptBuilder $luaScriptBuilder This property is read-only. - * - * @author Carsten Brandt - * @since 2.0 + * Xunsearch Connection + * @author xjflyttp */ class Connection extends Component { diff --git a/README.md b/README.md index ba7c6fa..a72e14d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,115 @@ yii2-xunsearch ============== -yii2 xunsearch +composer.json +---------------- +```json +"require": { + xj/yii2-xunsearch: "*" +}, +``` + +Configure Components +--------------- +```php +return [ + 'components' => [ + 'xunsearch' => [ + 'class' => 'xj\\xunsearch\\Connection', + //Put Xunsearch ini to $configDirectory + 'configDirectory' => '@common/config/xunsearch', + ], + ], +]; +``` + +Create ActiveRecord +--------------- +```php +class Demo extends \xj\xunsearch\ActiveRecord { + + public static function primaryKey() { + return ['pid']; + } + + public function rules() { + return [ + [['pid', 'subject', 'message'], 'required'] + ]; + } + + public function attributes() { + return [ + 'pid', 'subject', 'message', + ]; + } + +} +``` + +INSERT +-------------- +```php +$model = new Demo(); +$model->setAttributes([ + 'pid' => 1, + 'subject' => 'haha', + 'message' => 'hehe', +]); +$model->save(); +``` + +QUERY +--------------- +```php +//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(); +``` + +UPDATE +--------------- +```php +$model = Demo::findByPk(1); +$model->subject = 'mod subject'; +$model->save(); +``` + +DELETE +--------------- +```php +$model = Demo::findByPk(1); +$model->delete(); +``` + +COUNT +--------------- +```php +$count = Demo::find()->where([ + 'wild', 'key1', + ])->count(); +``` + +Work with ActiveDataProvider +---------------- +```php +$query = Demo::find(); +$dataProvider = new \yii\data\ActiveDataProvider([ + 'query' => $query, +]); +$models = $dataProvider->getModels(); +var_dump($models); +```