Yii-sorting

提供:Dev Guides
移動先:案内検索

Yii-ソート

大量のデータを表示する場合、多くの場合、データを並べ替える必要があります。 Yiiは* yii \ data \ Sortオブジェクト*を使用してソートスキーマを表します。

並べ替えの動作を示すには、データが必要です。

DBの準備

  • ステップ1 *-新しいデータベースを作成します。 データベースは、次の2つの方法で準備できます。
  • ターミナルで_mysql -u root –p_を実行します
  • _CREATE DATABASE helloworld CHARACTER SET utf8 COLLATE utf8_general_ci; _を介して新しいデータベースを作成します

ステップ2 *- *config/db.php ファイルでデータベース接続を設定します。 次の構成は、現在使用されているシステム用です。

<?php
   return [
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host=localhost;dbname=helloworld',
      'username' => 'vladimir',
      'password' => '12345',
      'charset' => 'utf8',
   ];
?>

ステップ3 *-ルートフォルダー内で./yii migrate/create test_table *を実行します。 このコマンドは、DBを管理するためのデータベース移行を作成します。 移行ファイルは、プロジェクトルートの *migrations フォルダーに表示されます。

ステップ4 *-この方法で移行ファイル(この場合は *m160106_163154_test_table.php )を変更します。

<?php
   use yii\db\Schema;
   use yii\db\Migration;
   class m160106_163154_test_table extends Migration {
      public function safeUp() {
         $this->createTable("user", [
            "id" => Schema::TYPE_PK,
            "name" => Schema::TYPE_STRING,
            "email" => Schema::TYPE_STRING,
         ]);
         $this->batchInsert("user", ["name", "email"], [
            ["User1", "[email protected]"],
            ["User2", "[email protected]"],
            ["User3", "[email protected]"],
            ["User4", "[email protected]"],
            ["User5", "[email protected]"],
            ["User6", "[email protected]"],
            ["User7", "[email protected]"],
            ["User8", "[email protected]"],
            ["User9", "[email protected]"],
            ["User10", "[email protected]"],
            ["User11", "[email protected]"],
         ]);
      }
      public function safeDown() {
         $this->dropTable('user');
      }
   }
?>

上記の移行では、id、name、およびemailのフィールドを持つ user テーブルが作成されます。 また、いくつかのデモユーザーを追加します。

  • ステップ5 *-プロジェクトのルート内で./yii migrate *を実行して、データベースに移行を適用します。

ステップ6 *-次に、 *user テーブルのモデルを作成する必要があります。 簡単にするために、 Gii コード生成ツールを使用します。 この url:http://localhost:8080/index.php?r = gii を開きます。 次に、「Model generator」ヘッダーの下にある「Start」ボタンをクリックします。 テーブル名(「ユーザー」)とモデルクラス(「MyUser」)を入力し、「プレビュー」ボタンをクリックして、最後に「生成」ボタンをクリックします。

DBの準備

MyUserモデルがモデルディレクトリに表示されます。

アクションでの並べ替え

ステップ1 *- *actionSorting メソッドを SiteController に追加します。

public function actionSorting() {
  //declaring the sort object
   $sort = new Sort([
      'attributes' => ['id', 'name', 'email'],
   ]);
  //retrieving all users
   $models = MyUser::find()
      ->orderBy($sort->orders)
      ->all();
   return $this->render('sorting', [
      'models' => $models,
      'sort' => $sort,
   ]);
}

ステップ2 *-views/siteフォルダー内で *sort 内に sort という View ファイルを作成します。

<?php
  //display links leading to sort actions
   echo $sort->link('id') . ' | ' . $sort->link('name') . ' | ' . $sort->link('email');
?><br/>
<?php foreach ($models as $model): ?>
   <?= $model->id; ?>
   <?= $model->name; ?>
   <?= $model->email; ?>
   <br/>
<?php endforeach; ?>

ステップ3 *-Webブラウザで *http://localhost:8080/index.php?r = site/sorting と入力すると、id、name、およびemailフィールドが次のようにソート可能であることがわかります。次の画像で。

アクションの並べ替え