Commit fdefbc71 by Qiang Xue

Reorganized guide.

parent 47c3f88b
This folder contains official Yii 2 guides documentation.
The Definitive Guide to Yii 2.0
===============================
To add new guide, take the following steps:
This tutorial is released under the [Terms of Yii Documentation](http://www.yiiframework.com/doc/terms/).
All Rights Reserved.
2014 (c) Yii Software LLC.
Introduction
------------
* [About Yii](intro-yii.md) - What is Yii and what is it good for?
* [Upgrading from Version 1.1](intro-upgrade-from-v1.md)
Getting Started
---------------
* **TBD** [Overview](start-overview.md) - Where should you start from?
* [Starting with Basic App](start-basic.md) - Best for developing a single-tier application by a single person
* [Starting with Advanced App](start-advanced.md) - Best for developing an enterprise application by a team
* [Starting from Scratch](start-scratch.md) - Learning more in-depth details of creating a Yii application step-by-step
Basic Concepts
--------------
* [Object Properties](basic-properties.md)
* [Events](basic-events.md)
* [Behaviors](basic-behaviors.md)
* [Object Configurations](basic-configs.md)
* **TBD** [Class Autoloading](basic-autoloading.md)
* **TBD** [Path Aliases](basic-alias.md)
* **TBD** [Extensions](basic-extensions.md)
* [Service Locator](basic-service-locator.md)
* **TBD** [Dependency Injection Container](basic-di-container.md)
Basic Structure
---------------
* [MVC Overview](structure-mvc.md)
* **TBD** [Entry Scripts](structure-entry-scripts.md)
* **TBD** [Applications](structure-applications.md)
* [Controllers and Actions](structure-controllers.md)
* [Views](structure-views.md)
* [Models](structure-models.md)
* **TBD** [Widgets](structure-widgets.md)
* **TBD** [Modules](structure-modules.md)
Handling Requests
-----------------
* **TBD** [Request Lifecycle](runtime-lifecycle.md)
* **TBD** [Bootstrapping](runtime-bootstrapping.md)
* **TBD** [Routing](runtime-routing.md)
* **TBD** [Requests](runtime-requests.md)
* **TBD** [Responses](runtime-responses.md)
* **TBD** [Sessions and Cookies](runtime-sessions-cookies.md)
* [URL Parsing and Generation](runtime-url-handling.md)
* **TBD** [Filtering](runtime-filtering.md)
Collecting Inputs
-----------------
* [Creating Forms](input-forms.md)
* [Input Validation](input-validation.md)
* **TBD** [Uploading Files](input-file-uploading.md)
* **TBD** [Inputs for Multiple Models](input-multiple-models.md)
Presenting Data
---------------
* **TBD** [Data Formatting](output-formatting.md)
* **TBD** [Pagination](output-pagination.md)
* **TBD** [Sorting](output-sorting.md)
* [Data Providers](output-data-providers.md)
* [Data Widgets](output-data-widgets.md)
* [Managing Assets](output-assets.md)
Working with Database
---------------------
* [Data Access Objects](db-dao.md) - Connecting to a database, basic queries, transactions and schema manipulation
* [Query Builder](db-query-builder.md) - Querying the database using a simple abstraction layer
* [Active Record](db-active-record.md) - The active record ORM, retrieving and manipulating records and defining relations
* [Migrations](db-migrations.md)
* **TBD** [Sphinx](db-sphinx.md)
* **TBD** [Redis](db-redis.md)
* **TBD** [MongoDB](db-mongodb.md)
* **TBD** [ElasticSearch](db-elastic-search.md)
Security
--------
* [Authentication](security-authentication.md)
* [Authorization](security-authorization.md)
* [Working with Passwords](security-passwords.md)
* **TBD** [Auth Clients](security-auth-clients.md)
* **TBD** [Best Practices](security-best-practices.md)
Caching
-------
* [Overview](caching-overview.md)
* **TBD** [Data Caching](caching-data.md)
* **TBD** [Fragment and Page Caching](caching-fragment.md)
* **TBD** [HTTP Caching](caching-http.md)
RESTful Web Services
--------------------
* [Quick Start](rest-quick-start.md)
* **TBD** [Resources](rest-resources.md)
* **TBD** [Routing](rest-routing.md)
* **TBD** [Data Formatting](rest-data-formatting.md)
* **TBD** [Authentication](rest-authentication.md)
* **TBD** [Rate Limiting](rest-rate-limiting.md)
* **TBD** [Versioning](rest-versioning.md)
* **TBD** [Caching](rest-caching.md)
* **TBD** [Error Handling](rest-error-handling.md)
* **TBD** [Testing](rest-testing.md)
Testing
-------
* [Overview](test-overview.md)
* **TBD** [Unit Tests](test-unit.md)
* **TBD** [Functional Tests](test-functional.md)
* **TBD** [Acceptance Tests](test-acceptance.md)
* [Fixtures](test-fixtures.md)
Extending Yii
-------------
* [Creating Extensions](extend-creating-extensions.md)
* [Customizing Core Code](extend-customizing-core.md)
* [Using 3rd-Party Libraries](extend-using-libs.md)
* **TBD** [Using Yii in 3rd-Party Systems](extend-embedding-in-others.md)
* **TBD** [Using Yii 1.1 and 2.0 Together](extend-using-v1-v2.md)
* [Using Composer](extend-using-composer.md)
Development Tools
-----------------
* [Debug Toolbar and Debugger](tool-debugger.md)
* [Generating Code using Gii](tool-gii.md)
* **TBD** [Generating API Documentation](tool-api-doc.md)
Special Topics
--------------
* [Configuring Web Servers](tutorial-configuring-servers.md)
* [Console Commands](tutorial-console.md)
* [Handling Errors](tutorial-handling-errors.md)
* [Internationalization](tutorial-i18n.md)
* [Logging](tutorial-logging.md)
* **TBD** [Mailing](tutorial-mailing.md)
* [Performance Tuning](tutorial-performance-tuning.md)
* [Template Engines](tutorial-template-engines.md)
* [Theming](tutorial-theming.md)
Widgets
-------
* GridView: link to demo page
* ListView: link to demo page
* DetailView: link to demo page
* ActiveForm: link to demo page
* Pjax: link to demo page
* Menu: link to demo page
* LinkPager: link to demo page
* LinkSorter: link to demo page
* [Bootstrap Widgets](bootstrap-widgets.md)
* **TBD** [Jquery UI Widgets](jui-widgets.md)
Helpers
-------
* **TBD** [ArrayHelper](helper-array.md)
* **TBD** [Html](helper-html.md)
* **TBD** [Url](helper-url.md)
* **TBD** [Security](helper-security.md)
1. Create `guide-name` and put there relevant documentation;
2. If guide has more then one word in name, then it should be with dashes, like: `console-fixture.md`, `module-debug.md`;
3. If your guide is about console commands, than its name should follow convention: `console-{command}.md`;
4. If your guide is about module usages, than its name should follow convention: `module-{moduleName}.md`.
5. If your guide is about widget usages, than its name should follow convention: `widget-{moduleName}.md`.
Managing Fixtures
=================
// todo: this tutorial may be merged into test-fixture.md
Fixtures are important part of testing. Their main purpose is to populate you with data that needed by testing
different cases. With this data using your tests becoming more efficient and useful.
Yii supports fixtures via the `yii fixture` command line tool. This tool supports:
* Loading fixtures to different storage such as: RDBMS, NoSQL, etc;
* Unloading fixtures in different ways (usually it is clearing storage);
* Auto-generating fixtures and populating it with random data.
Fixtures format
---------------
Fixtures are objects with different methods and configurations, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md) on them.
Lets assume we have fixtures data to load:
```
#users.php file under fixtures data path, by default @tests\unit\fixtures\data
return [
[
'name' => 'Chase',
'login' => 'lmayert',
'email' => 'strosin.vernice@jerde.com',
'auth_key' => 'K3nF70it7tzNsHddEiq0BZ0i-OU8S3xV',
'password' => '$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/iK0r3jRuwQEs2ldRu.a2',
],
[
'name' => 'Celestine',
'login' => 'napoleon69',
'email' => 'aileen.barton@heaneyschumm.com',
'auth_key' => 'dZlXsVnIDgIzFgX4EduAqkEPuphhOh9q',
'password' => '$2y$13$kkgpvJ8lnjKo8RuoR30ay.RjDf15bMcHIF7Vz1zz/6viYG5xJExU6',
],
];
```
If we are using fixture that loads data into database then these rows will be applied to `users` table. If we are using nosql fixtures, for example `mongodb`
fixture, then this data will be applied to `users` mongodb collection. In order to learn about implementing various loading strategies and more, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md).
Above fixture example was auto-generated by `yii2-faker` extension, read more about it in these [section](#auto-generating-fixtures).
Fixture classes name should not be plural.
Loading fixtures
----------------
Fixture classes should be suffixed by `Fixture` class. By default fixtures will be searched under `tests\unit\fixtures` namespace, you can
change this behavior with config or command options.
To load fixture, run the following command:
```
yii fixture/load <fixture_name>
```
The required `fixture_name` parameter specifies a fixture name which data will be loaded. You can load several fixtures at once.
Below are correct formats of this command:
```
// load `users` fixture
yii fixture/load User
// same as above, because default action of "fixture" command is "load"
yii fixture User
// load several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture User,UserProfile
// load all fixtures
yii fixture/load all
// same as above
yii fixture all
// load fixtures, but for other database connection.
yii fixture User --db='customDbConnectionId'
// load fixtures, but search them in different namespace. By default namespace is: tests\unit\fixtures.
yii fixture User --namespace='alias\my\custom\namespace'
// load global fixture `some\name\space\CustomFixture` before other fixtures will be loaded.
// By default this option is set to `InitDbFixture` to disable/enable integrity checks. You can specify several
// global fixtures separated by comma.
yii fixture User --globalFixtures='some\name\space\Custom'
```
Unloading fixtures
------------------
To unload fixture, run the following command:
```
// unload Users fixture, by default it will clear fixture storage (for example "users" table, or "users" collection if this is mongodb fixture).
yii fixture/unload User
// Unload several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture/unload User,UserProfile
// unload all fixtures
yii fixture/unload all
```
Same command options like: `db`, `namespace`, `globalFixtures` also can be applied to this command.
Configure Command Globally
--------------------------
While command line options allow us to configure the migration command
on-the-fly, sometimes we may want to configure the command once for all. For example you can configure
different migration path as follows:
```
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'db' => 'customDbConnectionId',
'namespace' => 'myalias\some\custom\namespace',
'globalFixtures' => [
'some\name\space\Foo',
'other\name\space\Bar'
],
],
]
```
Auto-generating fixtures
------------------------
Yii also can auto-generate fixtures for you based on some template. You can generate your fixtures with different data on different languages and formats.
These feature is done by [Faker](https://github.com/fzaninotto/Faker) library and `yii2-faker` extension.
See extension [guide](https://github.com/yiisoft/yii2/tree/master/extensions/faker) for more docs.
Data Sources and Widgets
========================
One of the most powerful features of Yii is how it works with data. In Yii, you may easily output data directly in view files, which is a good approach
for the Web site's frontend. But when it comes to backend data components and widgets, Yii's ability to access data is a real timesaver.
Typically, you would take the following steps when working with one of these data components:
1. Configure the [data provider](data-providers.md). It may get its data from an array, an SQL command, an ActiveRecord query, etc.
2. Pass the data provider to a widget, such as a [list view](data-widgets.md#listview) or [grid view](data-grid.md).
3. Customize the widget to reflect the presentational style that you are after.
That's it. After doing these simple steps you will have a powerful interfaces, such as a full featured data grid that supports pagination, sorting, and
filtering, ideal for the admin part of your project.
Data widgets
============
GridView
--------
The [[yii\grid\GridView]] widget is a powerful tool to create a data grid that provides pagination, sorting
and filtering of the data out of the box. See the [data grid section](data-grid.md) for more details.
ListView
--------
DetailView
----------
DetailView displays the detail of a single data [[yii\widgets\DetailView::$model|model]].
It is best used for displaying a model in a regular format (e.g. each model attribute is displayed as a row in a table).
The model can be either an instance of [[\yii\base\Model]] or an associative array.
DetailView uses the [[yii\widgets\DetailView::$attributes]] property to determines which model attributes should be displayed and how they
should be formatted.
A typical usage of DetailView is as follows:
```php
echo DetailView::widget([
'model' => $model,
'attributes' => [
'title', // title attribute (in plain text)
'description:html', // description attribute in HTML
[ // the owner name of the model
'label' => 'Owner',
'value' => $model->owner->name,
],
],
]);
```
The Definitive Guide to Yii 2.0
===============================
This tutorial is released under the [Terms of Yii Documentation](http://www.yiiframework.com/doc/terms/).
All Rights Reserved.
2014 (c) Yii Software LLC.
Introduction
------------
- [Overview](overview.md) - What is Yii and what is it good for?
- [Upgrading from 1.1 to 2.0](upgrade-from-v1.md)
Installation
------------
- [Installation](installation.md) - How to download Yii and configure the Web server?
- [Basic Application Template](apps-basic.md) - A template to start a basic frontend application.
- [Advanced Application Template](apps-advanced.md) - The basis for more advanced applications.
- [Creating your own Application structure](apps-own.md) - Learn how to start from scratch.
Base concepts
-------------
- [Basic Concepts](basics.md) - The Object and Component class, Path aliases and autoloading
- [Configuration](configuration.md) - Configuration of a Yii application
- [MVC](mvc.md) - Implementation of MVC in Yii and a typical MVC application flow
- [Model](model.md) - The Yii Model provides Attributes, Scenarios and data Validation
- [View](view.md) - Rendering Views applying layouts, using Widgets and asset management
- [Controller](controller.md) - controller actions, routing and action filters
- [Event Handling](events.md) - The Yii event handling mechanism
- [Behaviors](behaviors.md)
Database
--------
- [Basics](database-basics.md) - Connecting to a database, basic queries, transactions and schema manipulation
- [Query Builder](query-builder.md) - Querying the database using a simple abstraction layer
- [ActiveRecord](active-record.md) - The active record ORM, retrieving and manipulating records and defining relations
- [Database Migration](console-migrate.md) - Versioning your database with database migration
Developers Toolbox
------------------
- [Helper Classes](helpers.md)
- [Automatic Code Generation](gii.md)
- [Debug toolbar and debugger](module-debug.md)
- [Error Handling](error.md)
- [Logging](logging.md)
Extensions and 3rd party libraries
----------------------------------
- [Composer](composer.md) - How to manage applications dependencies via composer
- [Extending Yii](extensions.md)
- [Template engines](template.md) - Using template engines such as Smarty or Twig
- [Using Yii together with 3rd-Party Systems](using-3rd-party-libraries.md) - Using Yii in 3rd-Party Systems and using Yii 1 and 2 together
Security and access control
---------------------------
- [Authentication](authentication.md) - Identifying Users
- [Authorization](authorization.md) - Access control and RBAC
- [Security](security.md) - Hashing and verifying passwords, encryption
- [Views security](view.md#security) - how to prevent XSS
Data providers, lists and grids
-------------------------------
- [Overview](data-overview.md)
- [Data providers](data-providers.md)
- [Data widgets](data-widgets.md)
- [Grid](data-grid.md)
Advanced Topics
---------------
- [Asset Management](assets.md)
- [Working with forms](form.md)
- [Implementing RESTful Web Service APIs](rest.md)
- [Bootstrap widgets](bootstrap-widgets.md) - Using [twitter bootstrap](http://getbootstrap.com/)
- [Theming](theming.md)
- [Caching](caching.md) - Caching data, page fragments and http requests
- [Internationalization](i18n.md) - Message translation and formatting
- [URL Management](url.md) - routing, customized urls and SEO
- [Console Application](console.md)
- [Performance Tuning](performance.md)
- [Testing](testing.md)
- [Managing Test Fixtures](test-fixture.md)
- [Service Locator and Dependency Injection](di.md)
References
----------
- [Model validation reference](validation.md)
- [Official Composer documentation](http://getcomposer.org)
Introduction
------------
* [About Yii](intro-yii.md) - What is Yii and what is it good for?
* [Upgrading from Version 1.1](intro-upgrade-from-v1.md)
Getting Started
---------------
* [Overview](start-overview.md) - Where should you start from?
* [Starting with Basic App](start-basic.md) - Best for developing a single-tier application by a single person
* [Starting with Advanced App](start-advanced.md) - Best for developing an enterprise application by a team
* [Starting from Scratch](start-scratch.md) - Best for learning every bit of creating an application step-by-step
Basic Concepts
--------------
* [Object Properties](basic-properties.md)
* [Events](basic-events.md)
* [Behaviors](basic-behaviors.md)
* [Object Configurations](basic-configs.md)
* [Class Autoloading](basic-autoloading.md)
* [Path Aliases](basic-alias.md)
* [Extensions](basic-extensions.md)
* [Service Locator](basic-service-locator.md)
* [Dependency Injection Container](basic-di-container.md)
Key Components
--------------
* [MVC Overview](structure-mvc.md)
* [Entry Scripts](structure-entry-scripts.md)
* [Applications](structure-applications.md)
* [Controllers and Actions](structure-controllers.md)
* [Views](structure-views.md)
* [Models](structure-models.md)
* [Action Filters](structure-filters.md)
* [Widgets](structure-widgets.md)
* [Modules](structure-modules.md)
Handling Requests
-----------------
* [Request Lifecycle](runtime-lifecycle.md)
* [Bootstrapping](runtime-bootstrapping.md)
* [Routing](runtime-routing.md)
* [Requests](runtime-requests.md)
* [Responses](runtime-responses.md)
* [Sessions and Cookies](runtime-sessions-cookies.md)
* [URL Parsing and Generation](runtime-url-handling.md)
Collecting Input Data
---------------------
* [Creating Forms](input-forms.md)
* [Input Validation](input-validation.md)
* [Uploading Files](input-file-uploading.md)
* [Inputs for Multiple Models](input-multiple-models.md)
Presenting Data
---------------
* [Data Formatting](output-formatting.md)
* [Pagination](output-pagination.md)
* [Sorting](output-sorting.md)
* [Data Providers](output-data-providers.md)
* [Data Widgets](output-data-widgets.md)
* [Managing Assets](output-assets.md)
Working with Database
---------------------
* [Data Access Objects](db-dao.md) - Connecting to a database, basic queries, transactions and schema manipulation
* [Query Builder](db-query-builder.md) - Querying the database using a simple abstraction layer
* [Active Record](db-active-record.md) - The active record ORM, retrieving and manipulating records and defining relations
* [Migrations](db-migrations.md)
* [Sphinx](db-sphinx.md)
* [Redis](db-redis.md)
* [MongoDB](db-mongodb.md)
* [ElasticSearch](db-elastic-search.md)
Security
--------
* [Authentication](security-authentication.md)
* [Authorization](security-authorization.md)
* [Auth Clients](security-auth-clients.md)
* [Best Practices](security-best-practices.md)
Caching
-------
* [Overview](caching-overview.md)
* [Data Caching](caching-data.md)
* [Fragment and Page Caching](caching-fragment.md)
* [HTTP Caching](caching-http.md)
RESTful Web Services
--------------------
* [Quick Start](rest-quick-start.md)
* [Endpoints](rest-endpoints.md)
* [Resources](rest-resources.md)
* [Data Formatting](rest-data-formatting.md)
* [Authentication](rest-authentication.md)
* [Rate Limiting](rest-rate-limiting.md)
* [Versioning](rest-versioning.md)
* [Caching](rest-caching.md)
* [Error Handling](rest-error-handling.md)
* [Testing](rest-testing.md)
* [Documentation](rest-documentation.md)
Testing
-------
* [Overview](test-overview.md)
* [Unit Tests](test-unit.md)
* [Functional Tests](test-functional.md)
* [Acceptance Tests](test-acceptance.md)
* [Fixtures](test-fixtures.md)
Extending Yii
-------------
* [Creating Extensions](extend-creating-extensions.md)
* [Customizing Core Code](extend-customizing-core.md)
* [Using 3rd-Party Libraries](extend-using-libs.md)
* [Using Yii in 3rd-Party Systems](extend-embedding-in-others.md)
* [Using Yii 1.1 and 2.0 Together](extend-using-v1-v2.md)
Development Tools
-----------------
* [Debug Toolbar and Debugger](tool-debugger.md)
* [Generating Code using Gii](tool-gii.md)
* [Generating API Documentation](tool-apidoc.md)
* [Using Composer](tool-composer.md)
Special Topics
--------------
* [Console Commands](tutorial-console.md)
* [Handling Errors](tutorial-handling-errors.md)
* [Internationalization](tutorial-i18n.md)
* [Logging](tutorial-logging.md)
* [Mailing](tutorial-mailing.md)
* [Performance Tuning](tutorial-performance.md)
* [Template Engines](tutorial-template-engines.md)
* [Theming](tutorial-theming.md)
Widgets
-------
* GridView: link to demo page
* ListView: link to demo page
* DetailView: link to demo page
* ActiveForm: link to demo page
* Pjax: link to demo page
* Menu: link to demo page
* LinkPager: link to demo page
* LinkSorter: link to demo page
* [Bootstrap Widgets](bootstrap-widgets.md)
* [Jquery UI Widgets](jui-widgets.md)
Helpers
-------
* [ArrayHelper](helper-array.md)
* [Html](helper-html.md)
* [Url](helper-url.md)
* [Security](helper-security.md)
Data grid
=========
Data widgets
============
ListView
--------
DetailView
----------
DetailView displays the detail of a single data [[yii\widgets\DetailView::$model|model]].
It is best used for displaying a model in a regular format (e.g. each model attribute is displayed as a row in a table).
The model can be either an instance of [[\yii\base\Model]] or an associative array.
DetailView uses the [[yii\widgets\DetailView::$attributes]] property to determines which model attributes should be displayed and how they
should be formatted.
A typical usage of DetailView is as follows:
```php
echo DetailView::widget([
'model' => $model,
'attributes' => [
'title', // title attribute (in plain text)
'description:html', // description attribute in HTML
[ // the owner name of the model
'label' => 'Owner',
'value' => $model->owner->name,
],
],
]);
```
GridView
--------
Data grid or GridView is one of the most powerful Yii widgets. It is extremely useful if you need to quickly build admin
section of the system. It takes data from [data provider](data-providers.md) and renders each row using a set of columns
......@@ -32,8 +68,7 @@ echo GridView::widget([
The above code first creates a data provider and then uses GridView to display every attribute in every row taken from
data provider. The displayed table is equiped with sorting and pagination functionality.
Grid columns
------------
### Grid columns
Yii grid consists of a number of columns. Depending on column type and settings these are able to present data differently.
......@@ -147,9 +182,9 @@ rendered for the current row.
#### Checkbox column
CheckboxColumn displays a column of checkboxes.
To add a CheckboxColumn to the [[yii\grid\GridView]], add it to the [[yii\grid\GridView::$columns|columns]] configuration as follows:
```php
echo GridView::widget([
'dataProvider' => $dataProvider,
......@@ -324,3 +359,4 @@ In `search()` you then just add another filter condition with `$query->andFilter
> Info: For more information on `joinWith` and the queries performed in the background, check the
> [active record docs on eager and lazy loading](active-record.md#lazy-and-eager-loading).
......@@ -225,3 +225,138 @@ of running unit tests related with DB:
- Unload fixtures.
3. Repeat Step 2 until all tests finish.
**To be cleaned up below**
Managing Fixtures
=================
// todo: this tutorial may be merged into test-fixture.md
Fixtures are important part of testing. Their main purpose is to populate you with data that needed by testing
different cases. With this data using your tests becoming more efficient and useful.
Yii supports fixtures via the `yii fixture` command line tool. This tool supports:
* Loading fixtures to different storage such as: RDBMS, NoSQL, etc;
* Unloading fixtures in different ways (usually it is clearing storage);
* Auto-generating fixtures and populating it with random data.
Fixtures format
---------------
Fixtures are objects with different methods and configurations, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md) on them.
Lets assume we have fixtures data to load:
```
#users.php file under fixtures data path, by default @tests\unit\fixtures\data
return [
[
'name' => 'Chase',
'login' => 'lmayert',
'email' => 'strosin.vernice@jerde.com',
'auth_key' => 'K3nF70it7tzNsHddEiq0BZ0i-OU8S3xV',
'password' => '$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/iK0r3jRuwQEs2ldRu.a2',
],
[
'name' => 'Celestine',
'login' => 'napoleon69',
'email' => 'aileen.barton@heaneyschumm.com',
'auth_key' => 'dZlXsVnIDgIzFgX4EduAqkEPuphhOh9q',
'password' => '$2y$13$kkgpvJ8lnjKo8RuoR30ay.RjDf15bMcHIF7Vz1zz/6viYG5xJExU6',
],
];
```
If we are using fixture that loads data into database then these rows will be applied to `users` table. If we are using nosql fixtures, for example `mongodb`
fixture, then this data will be applied to `users` mongodb collection. In order to learn about implementing various loading strategies and more, refer to official [documentation](https://github.com/yiisoft/yii2/blob/master/docs/guide/test-fixture.md).
Above fixture example was auto-generated by `yii2-faker` extension, read more about it in these [section](#auto-generating-fixtures).
Fixture classes name should not be plural.
Loading fixtures
----------------
Fixture classes should be suffixed by `Fixture` class. By default fixtures will be searched under `tests\unit\fixtures` namespace, you can
change this behavior with config or command options.
To load fixture, run the following command:
```
yii fixture/load <fixture_name>
```
The required `fixture_name` parameter specifies a fixture name which data will be loaded. You can load several fixtures at once.
Below are correct formats of this command:
```
// load `users` fixture
yii fixture/load User
// same as above, because default action of "fixture" command is "load"
yii fixture User
// load several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture User,UserProfile
// load all fixtures
yii fixture/load all
// same as above
yii fixture all
// load fixtures, but for other database connection.
yii fixture User --db='customDbConnectionId'
// load fixtures, but search them in different namespace. By default namespace is: tests\unit\fixtures.
yii fixture User --namespace='alias\my\custom\namespace'
// load global fixture `some\name\space\CustomFixture` before other fixtures will be loaded.
// By default this option is set to `InitDbFixture` to disable/enable integrity checks. You can specify several
// global fixtures separated by comma.
yii fixture User --globalFixtures='some\name\space\Custom'
```
Unloading fixtures
------------------
To unload fixture, run the following command:
```
// unload Users fixture, by default it will clear fixture storage (for example "users" table, or "users" collection if this is mongodb fixture).
yii fixture/unload User
// Unload several fixtures. Note that there should not be any whitespace between ",", it should be one string.
yii fixture/unload User,UserProfile
// unload all fixtures
yii fixture/unload all
```
Same command options like: `db`, `namespace`, `globalFixtures` also can be applied to this command.
Configure Command Globally
--------------------------
While command line options allow us to configure the migration command
on-the-fly, sometimes we may want to configure the command once for all. For example you can configure
different migration path as follows:
```
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'db' => 'customDbConnectionId',
'namespace' => 'myalias\some\custom\namespace',
'globalFixtures' => [
'some\name\space\Foo',
'other\name\space\Bar'
],
],
]
```
Auto-generating fixtures
------------------------
Yii also can auto-generate fixtures for you based on some template. You can generate your fixtures with different data on different languages and formats.
These feature is done by [Faker](https://github.com/fzaninotto/Faker) library and `yii2-faker` extension.
See extension [guide](https://github.com/yiisoft/yii2/tree/master/extensions/faker) for more docs.
The Definitive Guide to Yii 2.0
===============================
This tutorial is released under [the Terms of Yii Documentation](http://www.yiiframework.com/doc/terms/).
All Rights Reserved.
&copy; 2014 Yii Software LLC.
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