Home > php教程 > PHP开发 > Yii2.0 uses database

Yii2.0 uses database

伊谢尔伦
Release: 2016-11-25 14:34:02
Original
1080 people have browsed it

Prepare the database:

Create a new database yii2basic, and then create a table in it:

CREATE TABLE `country` (
    `code` CHAR(2) NOT NULL PRIMARY KEY,
    `name` CHAR(52) NOT NULL,
    `population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `country` VALUES ('AU','Australia',18886000);
INSERT INTO `country` VALUES ('BR','Brazil',170115000);
INSERT INTO `country` VALUES ('CA','Canada',1147000);
INSERT INTO `country` VALUES ('CN','China',1277558000);
INSERT INTO `country` VALUES ('DE','Germany',82164700);
INSERT INTO `country` VALUES ('FR','France',59225700);
INSERT INTO `country` VALUES ('GB','United Kingdom',59623400);
INSERT INTO `country` VALUES ('IN','India',1013662000);
INSERT INTO `country` VALUES ('RU','Russia',146934000);
INSERT INTO `country` VALUES ('US','United States',278357000);
Copy after login

Configure the database connection:

In config/db.php:

<?php
return [
    &#39;class&#39; => &#39;yii\db\Connection&#39;,
    &#39;dsn&#39; => &#39;mysql:host=localhost;dbname=yii2basic&#39;,
    &#39;username&#39; => &#39;root&#39;,
    &#39;password&#39; => &#39;&#39;,
    &#39;charset&#39; => &#39;utf8&#39;,
];
Copy after login

Create an ActiveRecord:

models/Country. php:

<?php
    namespace app\models;
    use yii\db\ActiveRecord;
    class Country extends ActiveRecord
    {
    }
Copy after login

Create an Action:

controllers/CountryController.php:

<?php
    namespace app\controllers;
    use yii\web\Controller;
    use yii\data\Pagination;
    use app\models\Country;
    class CountryController extends Controller
    {
        public function actionIndex()
        {
            $query = Country::find();
            $pagination = new Pagination([
                &#39;defaultPageSize&#39; => 5,
                &#39;totalCount&#39; => $query->count(),
            ]);
        $countries = $query->orderBy(&#39;name&#39;)
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        return $this->render(&#39;index&#39;, [
            &#39;countries&#39; => $countries,
            &#39;pagination&#39; => $pagination,
        ]);
    }
}
Copy after login

Create a view

view/country/index.php:

<?php
    use yii\helpers\Html;
    use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
   <li>
       <?= Html::encode("{$country->name} ({$country->code})") ?>:
       <?= $country->population ?>
   </li>
<?php endforeach; ?>
</ul>

<?= LinkPager::widget([&#39;pagination&#39; => $pagination]) ?>
Copy after login

Try it:

http://hostname/index.php?r=country/index
Copy after login

Yii2.0 uses database


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template