mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 05:45:33 +08:00
Finished editing
This commit is contained in:
@@ -46,7 +46,7 @@ INSERT INTO `Country` VALUES ('US','United States',278357000);
|
||||
|
||||
At this point, you have a database named `yii2basic`, and within it a `country` table with three columns, containing ten rows of data.
|
||||
|
||||
Configuring the DB Connection <a name="configuring-db-connection"></a>
|
||||
Configuring a DB Connection <a name="configuring-db-connection"></a>
|
||||
---------------------------
|
||||
|
||||
Before proceeding, make sure you have installed both the [PDO](http://www.php.net/manual/en/book.pdo.php) PHP extension and
|
||||
@@ -129,10 +129,10 @@ You may find more detailed information in the [Active Record](db-active-record.m
|
||||
Creating an Action <a name="creating-action"></a>
|
||||
------------------
|
||||
|
||||
To expose the country data to end users, you need to create a new action. Instead of doing this in the `site`
|
||||
controller like you did in the previous sections, it makes more sense to create a new controller specifically
|
||||
for all actions about manipulating country data. Name this new controller as `CountryController` and create
|
||||
an `index` action in it, as shown in the following,
|
||||
To expose the country data to end users, you need to create a new action. Instead of placing the new action in the `site`
|
||||
controller, like you did in the previous sections, it makes more sense to create a new controller specifically
|
||||
for all actions related to the country data. Name this new controller `CountryController`, and create
|
||||
an `index` action in it, as shown in the following.
|
||||
|
||||
```php
|
||||
<?php
|
||||
@@ -169,25 +169,25 @@ class CountryController extends Controller
|
||||
|
||||
Save the above code in the file `controllers/CountryController.php`.
|
||||
|
||||
The `index` action calls `Country::find()` to build a DB query and retrieve all data from the `country` table.
|
||||
The `index` action calls `Country::find()`. This Active Record method builds a DB query and retrieves all of the data from the `country` table.
|
||||
To limit the number of countries returned in each request, the query is paginated with the help of a
|
||||
[[yii\data\Pagination]] object. The `Pagination` object serves for two purposes:
|
||||
[[yii\data\Pagination]] object. The `Pagination` object serves two purposes:
|
||||
|
||||
* Sets the `offset` and `limit` clauses for the SQL statement represented by the query so that it only
|
||||
returns a single page of data (at most 5 rows in a page).
|
||||
* Being used in the view to display a pager consisting of a list of page buttons, as will be explained in
|
||||
returns a single page of data at a time (at most 5 rows in a page).
|
||||
* It's used in the view to display a pager consisting of a list of page buttons, as will be explained in
|
||||
the next subsection.
|
||||
|
||||
At the end, the `index` action renders a view named `index` and passes the country data as well as the pagination
|
||||
At the end of the code, the `index` action renders a view named `index`, and passes the country data as well as the pagination
|
||||
information to it.
|
||||
|
||||
|
||||
Creating a View <a name="creating-view"></a>
|
||||
---------------
|
||||
|
||||
Under the `views` directory, first create a sub-directory named `country`. This will used to hold all
|
||||
Under the `views` directory, first create a sub-directory named `country`. This folder will used to hold all the
|
||||
views rendered by the `country` controller. Within the `views/country` directory, create a file named `index.php`
|
||||
with the following content:
|
||||
containing the following:
|
||||
|
||||
```php
|
||||
<?php
|
||||
@@ -207,7 +207,7 @@ use yii\widgets\LinkPager;
|
||||
<?= LinkPager::widget(['pagination' => $pagination]) ?>
|
||||
```
|
||||
|
||||
The view consists of two parts. In the first part, the country data is traversed and rendered as an unordered HTML list.
|
||||
The view has two sections relative to displaying the country data. In the first part, the provided country data is traversed and rendered as an unordered HTML list.
|
||||
In the second part, a [[yii\widgets\LinkPager]] widget is rendered using the pagination information passed from the action.
|
||||
The `LinkPager` widget displays a list of page buttons. Clicking on any of them will refresh the country data
|
||||
in the corresponding page.
|
||||
@@ -216,7 +216,7 @@ in the corresponding page.
|
||||
Trying it Out <a name="trying-it-out"></a>
|
||||
-------------
|
||||
|
||||
To see how it works, use your browser to access the following URL:
|
||||
To see how all of the above code works, use your browser to access the following URL:
|
||||
|
||||
```
|
||||
http://hostname/index.php?r=country/index
|
||||
@@ -224,34 +224,34 @@ http://hostname/index.php?r=country/index
|
||||
|
||||

|
||||
|
||||
You will see a page showing five countries. And below the countries, you will see a pager with four buttons.
|
||||
If you click on the button "2", you will see that the page displays another five countries in the database.
|
||||
Observe more carefully and you will find the URL in the browser changes to
|
||||
At first, you will see a page showing five countries. Below the countries, you will see a pager with four buttons.
|
||||
If you click on the button "2", you will see the page display another five countries in the database: the second page of records.
|
||||
Observe more carefully and you will find that the URL in the browser also changes to
|
||||
|
||||
```
|
||||
http://hostname/index.php?r=country/index&page=2
|
||||
```
|
||||
|
||||
Behind the scene, [[yii\data\Pagination|Pagination]] is playing the magic.
|
||||
Behind the scenes, [[yii\data\Pagination|Pagination]] is providing all of the ncessary functionality to paginate a data set:
|
||||
|
||||
* Initially, [[yii\data\Pagination|Pagination]] represents the first page, which sets the country query
|
||||
* Initially, [[yii\data\Pagination|Pagination]] represents the first page, which reflects the country SELECT query
|
||||
with the clause `LIMIT 5 OFFSET 0`. As a result, the first five countries will be fetched and displayed.
|
||||
* The [[yii\widgets\LinkPager|LinkPager]] widget renders the page buttons using the URLs
|
||||
created by [[yii\data\Pagination::createUrl()|Pagination]]. The URLs will contain the query parameter `page`
|
||||
representing different page numbers.
|
||||
created by [[yii\data\Pagination::createUrl()|Pagination]]. The URLs will contain the query parameter `page`, which
|
||||
represents the different page numbers.
|
||||
* If you click the page button "2", a new request for the route `country/index` will be triggered and handled.
|
||||
[[yii\data\Pagination|Pagination]] reads the `page` query parameter and sets the current page number 2.
|
||||
The new country query will thus have the clause `LIMIT 5 OFFSET 5` and return back the next five countries
|
||||
[[yii\data\Pagination|Pagination]] reads the `page` query parameter from the URL and sets the current page number to 2.
|
||||
The new country query will thus have the clause `LIMIT 5 OFFSET 5` and return the next five countries
|
||||
for display.
|
||||
|
||||
|
||||
Summary <a name="summary"></a>
|
||||
-------
|
||||
|
||||
In this section, you have learned how to work with a database. You have also learned how to fetch and display
|
||||
In this section, you learned how to work with a database. You also learned how to fetch and display
|
||||
data in pages with the help of [[yii\data\Pagination]] and [[yii\widgets\LinkPager]].
|
||||
|
||||
In the next section, you will learn how to use the powerful code generation tool, called [Gii](tool-gii.md),
|
||||
to help you rapidly implement some commonly required features, such as the Create-Read-Update-Delete (CRUD)
|
||||
operations about the data in a DB table. As a matter of fact, the code you have just written can all
|
||||
be automatically generated using this tool.
|
||||
operations for working with the data in a database table. As a matter of fact, the code you have just written can all
|
||||
be automatically generated in Yii using the Gii tool.
|
||||
|
||||
Reference in New Issue
Block a user