Browse Source

update db-active-record translation

tags/2.0.16
deepziyu 7 years ago committed by GitHub
parent
commit
a99007ed43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 64
      docs/guide-zh-CN/db-active-record.md

64
docs/guide-zh-CN/db-active-record.md

@ -219,21 +219,21 @@ $customers = Customer::findAll([
]);
```
> Warning: If you need to pass user input to these methods, make sure the input value is scalar or in case of
> array condition, make sure the array structure can not be changed from the outside:
> 注:如果你需要将用户输入传递给这些方法,请确保输入值是标量或者是
> 数组条件,确保数组结构不能被外部所改变:
>
> ```php
> // yii\web\Controller ensures that $id is scalar
> // yii\web\Controller 确保了 $id 是标量
> public function actionView($id)
> {
> $model = Post::findOne($id);
> // ...
> }
>
> // explicitly specifying the column to search, passing a scalar or array here will always result in finding a single record
> // 明确了指定要搜索的列,在此处传递标量或数组将始终只是查找出单个记录而已
> $model = Post::findOne(['id' => Yii::$app->request->get('id')]);
>
> // do NOT use the following code! it is possible to inject an array condition to filter by arbitrary column values!
> // 不要使用下面的代码!可以注入一个数组条件来匹配任意列的值!
> $model = Post::findOne(Yii::$app->request->get('id'));
> ```
@ -510,34 +510,34 @@ $customer->loadDefaultValues();
> Tip: 你可以使用 [[yii\behaviors\AttributeTypecastBehavior]] 来简化属性的类型转换
在 ActiveRecord 验证或者保存过程中。
Since 2.0.14, Yii ActiveRecord supports complex data types, such as JSON or multidimensional arrays.
从2.0.14开始,Yii ActiveRecord 支持了更多的复杂数据类型,例如 JSON 或多维数组。
#### JSON in MySQL and PostgreSQL
#### MySQL 和 PostgreSQL 的 JSON 类型 in MySQL and PostgreSQL
After data population, the value from JSON column will be automatically decoded from JSON according to standard JSON
decoding rules.
(在 ActiveRecord 的)数据填充后,基于 JSON 标准解码规则,来自 JSON 列的值将自动解码。
To save attribute value to a JSON column, ActiveRecord will automatically create a [[yii\db\JsonExpression|JsonExpression]] object
that will be encoded to a JSON string on [QueryBuilder](db-query-builder.md) level.
#### Arrays in PostgreSQL
另一方面,为了将属性值保存到 JSON 列中,ActiveRecord 会自动创建一个 [[yii\db\JsonExpression|JsonExpression]] 对象,
这对象将在 [QueryBuilder](db-query-builder.md) 层被编码成 JSON 字符串。
After data population, the value from Array column will be automatically decoded from PgSQL notation to an [[yii\db\ArrayExpression|ArrayExpression]]
object. It implements PHP `ArrayAccess` interface, so you can use it as an array, or call `->getValue()` to get the array itself.
#### PostgreSQL 的数组类型
To save attribute value to an array column, ActiveRecord will automatically create an [[yii\db\ArrayExpression|ArrayExpression]] object
that will be encoded by [QueryBuilder](db-query-builder.md) to an PgSQL string representation of array.
(在 ActiveRecord 的)数据填充后,来自 Array 列的值将自动从 PgSQL 的编码值解码为 一个 [[yii\db\ArrayExpression|ArrayExpression]]
对象。它继承于 PHP 的 `ArrayAccess` 接口,所以你可以把它当作一个数组用,或者调用 `->getValue()` 来获取数组本身。
You can also use conditions for JSON columns:
另一方面,为了将属性值保存到数组列,ActiveRecord 会自动创建一个 [[yii\db\ArrayExpression|ArrayExpression]] 对象,
这对象将在 [QueryBuilder](db-query-builder.md) 中被编码成数组的 PgSQL 字符串表达式。
你还可以这样使用 JSON 列的条件:
```php
$query->andWhere(['=', 'json', new ArrayExpression(['foo' => 'bar'])
```
To learn more about expressions building system read the [Query Builder – Adding custom Conditions and Expressions](db-query-builder.md#adding-custom-conditions-and-expressions)
article.
要详细了解表达式构建系统,可以访问 [查询构建器 – 增加自定义条件和语句](db-query-builder.md#adding-custom-conditions-and-expressions)
文章。
### Updating Multiple Rows <span id="updating-multiple-rows"></span>
### 更新多个数据行 <span id="updating-multiple-rows"></span>
上述方法都可以用于单个 AR 实例,以插入或更新单条
表数据行。 要同时更新多个数据行,你应该调用 [[yii\db\ActiveRecord::updateAll()|updateAll()]]
@ -981,12 +981,12 @@ $items = $order->items;
```
### Chaining relation definitions via multiple tables <span id="multi-table-relations"></span>
### 通过多个(中间表)表来连接关联声明 <span id="multi-table-relations"></span>
Its further possible to define relations via multiple tables by chaining relation definitions using [[yii\db\ActiveQuery::via()|via()]].
Considering the examples above, we have classes `Customer`, `Order`, and `Item`.
We can add a relation to the `Customer` class that lists all items from all the orders they placed,
and name it `getPurchasedItems()`, the chaining of relations is show in the following code example:
通过使用 [[yii\db\ActiveQuery::via()|via()]] 方法,它还可以通过多个表来定义关联声明。
再考虑考虑上面的例子,我们有 `Customer`, `Order`, 和 `Item` 类。
我们可以添加一个关联关系到 `Customer` 类,这个关联可以列出了 `Customer`(客户) 的订单下放置的所有 `Item`(商品),
这个关联命名为 `getPurchasedItems()`,关联声明如下代码示例所示:
```php
class Customer extends ActiveRecord
@ -995,21 +995,21 @@ class Customer extends ActiveRecord
public function getPurchasedItems()
{
// customer's items, matching 'id' column of `Item` to 'item_id' in OrderItem
// 客户的商品,将 Item 中的 'id' 列与 OrderItem 中的 'item_id' 相匹配
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems');
}
public function getOrderItems()
{
// customer's order items, matching 'id' column of `Order` to 'order_id' in OrderItem
// 客户订单中的商品,将 `Order` 的 'id' 列和 OrderItem 的 'order_id' 列相匹配
return $this->hasMany(OrderItem::className(), ['order_id' => 'id'])
->via('orders');
}
public function getOrders()
{
// same as above
// 见上述列子
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
@ -1089,10 +1089,10 @@ $orders= $customers[0]->orders;
// 没有任何的 SQL 执行
$country = $customers[0]->country;
// eager loading "orders" and the nested relation "orders.items"
// 即时加载“订单”和嵌套关系“orders.items”
$customers = Customer::find()->with('orders.items')->all();
// access the items of the first order of the first customer
// no SQL executed
// 访问第一个客户的第一个订单中的商品
// 没有 SQL 查询执行
$items = $customers[0]->orders[0]->items;
```
@ -1181,7 +1181,7 @@ $customers = Customer::find()
调用 [[yii\db\ActiveQuery::joinWith()|joinWith()]] 方法会默认 [即时加载](#lazy-eager-loading) 相应的关联数据。
如果你不需要那些关联数据,你可以指定它的第二个参数 $eagerLoading` 为 `false`
> Note: Even when using [[yii\db\ActiveQuery::joinWith()|joinWith()]] or [[yii\db\ActiveQuery::innerJoinWith()|innerJoinWith()]] with eager loading enabled the related data will **not** be populated from the result of the `JOIN` query. So there's still an extra query for each joined relation as explained in the section on [eager loading](#lazy-eager-loading).
> 提示:即使在启用即时加载的情况下使用 [[yii\db\ActiveQuery::joinWith()|joinWith()]] 或 [[yii\db\ActiveQuery::innerJoinWith()|innerJoinWith()]],相应的关联数据也**不会**从这个 `JOIN` 查询的结果中填充。 因此,每个连接关系还有一个额外的查询,正如 [即时加载](#lazy-eager-loading) 部分所述。
和 [[yii\db\ActiveQuery::with()|with()]] 一样,你可以 join 多个关联表;你可以动态的自定义
你的关联查询;你可以使用嵌套关联进行 join。你也可以将 [[yii\db\ActiveQuery::with()|with()]]

Loading…
Cancel
Save