Commit 4fdfe7a9 by Qiang Xue

Merge branch 'master' of git://github.com/yiisoft/yii2

parents 31454eef 1765efce
...@@ -387,7 +387,7 @@ $sql = $command->sql; ...@@ -387,7 +387,7 @@ $sql = $command->sql;
$rows = $command->queryAll(); $rows = $command->queryAll();
``` ```
Лучшим спосом использования данных методов является работа с [Active Record](db-active-record.md). Лучшим способом использования данных методов является работа с [Active Record](db-active-record.md).
Более детальная информация представлена в разделе [Построитель запросов](db-query-builder.md). Более детальная информация представлена в разделе [Построитель запросов](db-query-builder.md).
...@@ -436,7 +436,7 @@ Yii 2.0 осуществляет жадную загрузку (eager loading) ...@@ -436,7 +436,7 @@ Yii 2.0 осуществляет жадную загрузку (eager loading)
Вместо того, чтобы возвращать объекты [[yii\db\ActiveRecord|ActiveRecord]], вы можете использовать метод [[yii\db\ActiveQuery::asArray()|asArray()]] Вместо того, чтобы возвращать объекты [[yii\db\ActiveRecord|ActiveRecord]], вы можете использовать метод [[yii\db\ActiveQuery::asArray()|asArray()]]
при построении запроса, для выборки большого количества записей. Это заставит вернуть результат запроса в качестве массива, что при построении запроса, для выборки большого количества записей. Это заставит вернуть результат запроса в качестве массива, что
может существеннос снизить время, нужное ЦПУ и память, при большом количестве записей. Например: может существенно снизить время, нужное ЦПУ и память, при большом количестве записей. Например:
```php ```php
$customers = Customer::find()->asArray()->all(); $customers = Customer::find()->asArray()->all();
......
...@@ -2,7 +2,7 @@ Class Autoloading ...@@ -2,7 +2,7 @@ Class Autoloading
================= =================
Yii relies on the [class autoloading mechanism](http://www.php.net/manual/en/language.oop5.autoload.php) Yii relies on the [class autoloading mechanism](http://www.php.net/manual/en/language.oop5.autoload.php)
to locate and include required class files. It provides a high-performance class autoloader that is compliant to the to locate and include all required class files. It provides a high-performance class autoloader that is compliant to the
[PSR-4 standard](https://github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md). [PSR-4 standard](https://github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md).
The autoloader is installed when you include the `Yii.php` file. The autoloader is installed when you include the `Yii.php` file.
...@@ -15,7 +15,7 @@ Using the Yii Autoloader <a name="using-yii-autoloader"></a> ...@@ -15,7 +15,7 @@ Using the Yii Autoloader <a name="using-yii-autoloader"></a>
To make use of the Yii class autoloader, you should follow two simple rules when creating and naming your classes: To make use of the Yii class autoloader, you should follow two simple rules when creating and naming your classes:
* Each class must be under some namespace (e.g. `foo\bar\MyClass`). * Each class must be under a namespace (e.g. `foo\bar\MyClass`)
* Each class must be saved in an individual file whose path is determined by the following algorithm: * Each class must be saved in an individual file whose path is determined by the following algorithm:
```php ```php
...@@ -23,30 +23,30 @@ To make use of the Yii class autoloader, you should follow two simple rules when ...@@ -23,30 +23,30 @@ To make use of the Yii class autoloader, you should follow two simple rules when
$classFile = Yii::getAlias('@' . str_replace('\\', '/', $className) . '.php'); $classFile = Yii::getAlias('@' . str_replace('\\', '/', $className) . '.php');
``` ```
For example, if a class name is `foo\bar\MyClass`, the [alias](concept-aliases.md) for the corresponding class file path For example, if a class name and namespace is `foo\bar\MyClass`, the [alias](concept-aliases.md) for the corresponding class file path
would be `@foo/bar/MyClass.php`. In order for this alias to be able to be resolved into a file path, would be `@foo/bar/MyClass.php`. In order for this alias to be resolvable into a file path,
either `@foo` or `@foo/bar` must be a [root alias](concept-aliases.md#defining-aliases). either `@foo` or `@foo/bar` must be a [root alias](concept-aliases.md#defining-aliases).
When you are using the [Basic Application Template](start-basic.md), you may put your classes under the top-level When using the [Basic Application Template](start-basic.md), you may put your classes under the top-level
namespace `app` so that they can be autoloaded by Yii without the need of defining a new alias. This is because namespace `app` so that they can be autoloaded by Yii without the need of defining a new alias. This is because
`@app` is a [predefined alias](concept-aliases.md#predefined-aliases), and a class name like `app\components\MyClass` `@app` is a [predefined alias](concept-aliases.md#predefined-aliases), and a class name like `app\components\MyClass`
can be resolved into the class file `AppBasePath/components/MyClass.php`, according to the algorithm we just described. can be resolved into the class file `AppBasePath/components/MyClass.php`, according to the algorithm just described.
In the [Advanced Application Template](tutorial-advanced-app.md), each tier has its own root alias. For example, In the [Advanced Application Template](tutorial-advanced-app.md), each tier has its own root alias. For example,
the front-end tier has a root alias `@frontend` while the back-end tier `@backend`. As a result, you may the front-end tier has a root alias `@frontend`, while the back-end tier `@backend`. As a result, you may
put the front-end classes under the namespace `frontend` while the back-end classes under `backend`. This will put the front-end classes under the namespace `frontend` while the back-end classes are under `backend`. This will
allow these classes to be autoloaded by the Yii autoloader. allow these classes to be autoloaded by the Yii autoloader.
Class Map <a name="class-map"></a> Class Map <a name="class-map"></a>
--------- ---------
The Yii class autoloader supports the *class map* feature which maps class names to the corresponding class file paths. The Yii class autoloader supports the *class map* feature, which maps class names to the corresponding class file paths.
When the autoloader is loading a class, it will first check if the class is found in the map. If so, the corresponding When the autoloader is loading a class, it will first check if the class is found in the map. If so, the corresponding
file path will be included directly without further check. This makes class autoloading super fast. In fact, file path will be included directly without further check. This makes class autoloading super fast. In fact,
all core Yii classes are being autoloaded this way. all core Yii classes are autoloaded this way.
You may add a class to the class map `Yii::$classMap` as follows, You may add a class to the class map, stored in `Yii::$classMap`, using:
```php ```php
Yii::$classMap['foo\bar\MyClass'] = 'path/to/MyClass.php'; Yii::$classMap['foo\bar\MyClass'] = 'path/to/MyClass.php';
...@@ -60,14 +60,14 @@ Using Other Autoloaders <a name="using-other-autoloaders"></a> ...@@ -60,14 +60,14 @@ Using Other Autoloaders <a name="using-other-autoloaders"></a>
----------------------- -----------------------
Because Yii embraces Composer as a package dependency manager, it is recommended that you also install Because Yii embraces Composer as a package dependency manager, it is recommended that you also install
the Composer autoloader. If you are using some 3rd-party libraries that have their autoloaders, you should the Composer autoloader. If you are using 3rd-party libraries that have their own autoloaders, you should
also install them. also install those.
When you are using the Yii autoloader together with other autoloaders, you should include the `Yii.php` file When using the Yii autoloader together with other autoloaders, you should include the `Yii.php` file
*after* all other autoloaders are installed. This will make the Yii autoloader to be the first one responding to *after* all other autoloaders are installed. This will make the Yii autoloader the first one responding to
any class autoloading request. For example, the following code is extracted from any class autoloading request. For example, the following code is extracted from
the [entry script](structure-entry-scripts.md) of the [Basic Application Template](start-basic.md). The first the [entry script](structure-entry-scripts.md) of the [Basic Application Template](start-basic.md). The first
line installs the Composer autoloader, while the second line installs the Yii autoloader. line installs the Composer autoloader, while the second line installs the Yii autoloader:
```php ```php
require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/autoload.php');
......
...@@ -2,19 +2,19 @@ Service Locator ...@@ -2,19 +2,19 @@ Service Locator
=============== ===============
A service locator is an object that knows how to provide all sorts of services (or components) that an application A service locator is an object that knows how to provide all sorts of services (or components) that an application
might need. Within a service locator, each component has only a single instance which is uniquely identified by an ID. might need. Within a service locator, each component exists as only a single instance, uniquely identified by an ID.
You use the ID to retrieve a component from the service locator. You use the ID to retrieve a component from the service locator.
In Yii, a service locator is simply an instance of [[yii\di\ServiceLocator]] or its child class. In Yii, a service locator is simply an instance of [[yii\di\ServiceLocator]], or from a child class.
The most commonly used service locator in Yii is the *application* object which can be accessed through The most commonly used service locator in Yii is the *application* object, which can be accessed through
`\Yii::$app`. The services it provides are called *application components*, such as the `request`, `response`, `\Yii::$app`. The services it provides are called *application components*, such as the `request`, `response`, and
`urlManager` components. You may configure these components or even replace them with your own implementations easily `urlManager` components. You may configure these components, or even replace them with your own implementations, easily
through functionality provided by the service locator. through functionality provided by the service locator.
Besides the application object, each module object is also a service locator. Besides the application object, each module object is also a service locator.
To use a service locator, the first step is to register components. A component can be registered To use a service locator, the first step is to register components with it. A component can be registered
via [[yii\di\ServiceLocator::set()]]. The following code shows different ways of registering components: via [[yii\di\ServiceLocator::set()]]. The following code shows different ways of registering components:
```php ```php
...@@ -43,7 +43,7 @@ $locator->set('search', function () { ...@@ -43,7 +43,7 @@ $locator->set('search', function () {
$locator->set('pageCache', new FileCache); $locator->set('pageCache', new FileCache);
``` ```
Once a component is registered, you can access it using its ID in one of the following two ways: Once a component has been registered, you can access it using its ID, in one of the two following ways:
```php ```php
$cache = $locator->get('cache'); $cache = $locator->get('cache');
...@@ -53,7 +53,7 @@ $cache = $locator->cache; ...@@ -53,7 +53,7 @@ $cache = $locator->cache;
As shown above, [[yii\di\ServiceLocator]] allows you to access a component like a property using the component ID. As shown above, [[yii\di\ServiceLocator]] allows you to access a component like a property using the component ID.
When you access a component for the first time, [[yii\di\ServiceLocator]] will use the component registration When you access a component for the first time, [[yii\di\ServiceLocator]] will use the component registration
information to create a new instance of the component and return it. Later if the component is accessed again, information to create a new instance of the component and return it. Later, if the component is accessed again,
the service locator will return the same instance. the service locator will return the same instance.
You may use [[yii\di\ServiceLocator::has()]] to check if a component ID has already been registered. You may use [[yii\di\ServiceLocator::has()]] to check if a component ID has already been registered.
...@@ -61,9 +61,8 @@ If you call [[yii\di\ServiceLocator::get()]] with an invalid ID, an exception wi ...@@ -61,9 +61,8 @@ If you call [[yii\di\ServiceLocator::get()]] with an invalid ID, an exception wi
Because service locators are often being created with [configurations](concept-configurations.md), Because service locators are often being created with [configurations](concept-configurations.md),
a writable property named [[yii\di\ServiceLocator::setComponents()|components]] is provided so that a writable property named [[yii\di\ServiceLocator::setComponents()|components]] is provided. This allows you to configure and register multiple components at once. The following code shows a configuration array
you can configure it and register multiple components at once. The following code shows a configuration array that can be used to configure an application, while also registering the "db", "cache" and "search" components:
that can be used to configure an application and register the "db", "cache" and "search" components:
```php ```php
return [ return [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment