Skip to content

Commit

Permalink
updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
xjflyttp committed Aug 6, 2014
1 parent 30686e6 commit 8abe146
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 80 deletions.
68 changes: 23 additions & 45 deletions ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ------------
*
Expand All @@ -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 <mail@cebe.cc>
* @since 2.0
* @author xjflyttp <xjflyttp@gmail.com>
*/
class ActiveQuery extends Component implements ActiveQueryInterface {

Expand All @@ -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.
Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -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";
Expand Down
35 changes: 14 additions & 21 deletions ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mail@cebe.cc>
* @since 2.0
* Xunsearch AR
* @author xjflyttp <xjflyttp@gmail.com>
*/
class ActiveRecord extends BaseActiveRecord {

Expand All @@ -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.
Expand Down
15 changes: 2 additions & 13 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mail@cebe.cc>
* @since 2.0
* Xunsearch Connection
* @author xjflyttp <xjflyttp@gmail.com>
*/
class Connection extends Component {

Expand Down
113 changes: 112 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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);
```

0 comments on commit 8abe146

Please sign in to comment.