Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LuaScriptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private function quoteValue($str)
return $str;
}

return "'" . addcslashes(str_replace("'", "\\'", $str), "\000\n\r\\\032") . "'";
return "'" . addcslashes($str, "\000\n\r\\\032\047") . "'";
}

/**
Expand Down
22 changes: 22 additions & 0 deletions docs/guide-zh-CN/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
用于 Yii 2 的 Redis 缓存、会话和活动记录(ActiveRecord)扩展
============================================================

此扩展为 Yii 2 框架提供 [redis](http://redis.io/) 键值存储支持。
它包含了 `缓存` 和 `会话` 存储句柄,并实现了 `ActiveRecord` 模式,以便您能够在 redis 中保存活动记录。

起步
----

* [安装](installation.md)

用法
----

* [使用活动记录](usage-ar.md)
* [直接使用命令](usage-commands.md)

额外主题
--------

* [使用缓存组件](topics-cache.md)
* [使用会话组件](topics-session.md)
41 changes: 41 additions & 0 deletions docs/guide-zh-CN/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
安装
====

## 需求

redis 版本至少为 2.6.12,以供所有组件能够正常工作。

## 获取 Composer 包

最合适的安装方法是通过 [composer](http://getcomposer.org/download/) 安装该扩展。

运行

```
php composer.phar require --prefer-dist yiisoft/yii2-redis
```

或在你的 composer.json 文件的“require”一节添加以下代码:

```json
"yiisoft/yii2-redis": "~2.0.0"
```


## 配置应用程序

要使用该组件,你应当在你的应用程序配置中配置 Connection 类:

```php
return [
//....
'components' => [
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
]
];
```
37 changes: 37 additions & 0 deletions docs/guide-zh-CN/topics-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
使用缓存组件
============

要使用 `缓存` 组件,您不仅要按照[安装](installation.md) 一节的描述配置连接,还要
将 `缓存` 组件配置为 `yii\redis\Cache`:

```php
return [
//....
'components' => [
// ...
'cache' => [
'class' => 'yii\redis\Cache',
],
]
];
```

若您仅使用 redis 缓存(比如不使用其活动记录或会话组件),你也可以在缓存组件配置中
附加连接参数(这种情况下不需要单独配置连接应用程序组件):

```php
return [
//....
'components' => [
// ...
'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
];
```
37 changes: 37 additions & 0 deletions docs/guide-zh-CN/topics-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
使用会话组件
============

要使用 `会话` 组件,您不仅要按照[安装](installation.md) 一节的描述配置连接,还要
将 `会话` 组件配置为 `yii\redis\Session`:

```php
return [
//....
'components' => [
// ...
'session' => [
'class' => 'yii\redis\Session',
],
]
];
```

若您仅使用 redis 会话(比如不使用其活动记录或缓存组件),你也可以在会话组件配置中
附加连接参数(这种情况下不需要单独配置连接应用程序组件):

```php
return [
//....
'components' => [
// ...
'session' => [
'class' => 'yii\redis\Session',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
],
]
];
```
64 changes: 64 additions & 0 deletions docs/guide-zh-CN/usage-ar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
使用活动记录
============

要了解如何使用 Yii 的活动记录,请参考 [导览](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md).

要定义一个 redis 活动记录类,则您的记录类需要继承自 [[yii\redis\ActiveRecord]] 并且
至少要实现 `attributes()` 方法,以定义记录的属性。
可以通过覆盖 [[yii\redis\ActiveRecord::primaryKey()]] 定义主键,若不定义,则默认为 `id`。
主键必须是属性之一,所以,若您没有定义主键时,则须确保属性列表中定义了 `id` 属性。

以下为示例模型 `Customer`:

```php
class Customer extends \yii\redis\ActiveRecord
{
/**
* @return array 该记录的属性列表
*/
public function attributes()
{
return ['id', 'name', 'address', 'registration_date'];
}

/**
* @return ActiveQuery 定义一个关联 Order 记录的关系(可以是其它数据库,如 ElasticSearch 或 关系型数据库)
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}

/**
* Defines a scope that modifies the `$query` to return only active(status = 1) customers
*/
public static function active($query)
{
$query->andWhere(['status' => 1]);
}
}
```

活动记录的一般用法与
[导览](https://github.com/yiisoft/yii2/blob/master/docs/guide/active-record.md)
中描述的数据库的活动记录用法类似。
它们支持相同的接口和特性,但有以下限制:

- 由于 redis 不支持 SQL,查询 API 限于以下若干方法:
`where()`、`limit()`、`offset()`、`orderBy()` 和 `indexBy()`。
(orderBy() 还未实现:[#1305](https://github.com/yiisoft/yii2/issues/1305))
- `via`- 关系不能定义为不在 redis 中的表。您只能定义其它记录。

您也可以定义从 redis 活动记录到常规数据库活动记录类的关系,反之亦然。

用法举例:

```php
$customer = new Customer();
$customer->attributes = ['name' => 'test'];
$customer->save();
echo $customer->id; // 如果不严格设置 id 值,则该值会自动递增。

$customer = Customer::find()->where(['name' => 'test'])->one(); // find by query
$customer = Customer::find()->active()->all(); // find all by query (using the `active` scope)
```
23 changes: 23 additions & 0 deletions docs/guide-zh-CN/usage-commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
直接使用命令
============

Redis 拥有诸多非常有用的命令,而且这些命令可以直接从连接组件中执行。按照 [安装](installation.md) 中的
描述配置好应用程序后,可以按以下方法获得`连接`:

```php
$redis = Yii::$app->redis;
```

获得后便可以执行命令。最常用的方法是使用其 `executeCommand` 方法:

```php
$result = $redis->executeCommand('hmset', ['test_collection', 'key1', 'val1', 'key2', 'val2']);
```

某些命令有其快捷方式,而不必使用以上方式,示例如下:

```php
$result = $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2']);
```

快捷方式命令列表和它们的参数详见 [http://redis.io/commands](http://redis.io/commands)。