Yii-creating-event
Yii-イベントの作成
この章では、Yiiでイベントを作成する方法を説明します。 実行中のイベントを表示するには、データが必要です。
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」)を入力し、「プレビュー」ボタンをクリックして、最後に「生成」ボタンをクリックします。
MyUserモデルがモデルディレクトリに表示されます。
イベントを作成する
新しいユーザーがWebサイトに登録するたびに、管理者にメールを送信するとします。
ステップ1 *-このように *models/MyUser.php ファイルを変更します。
<?php
namespace app\models;
use Yii;
/**
*This is the model class for table "user".
*
*@property integer $id
* @property string $name
*@property string $email
*/
class MyUser extends \yii\db\ActiveRecord {
const EVENT_NEW_USER = 'new-user';
public function init() {
//first parameter is the name of the event and second is the handler.
$this->on(self::EVENT_NEW_USER, [$this, 'sendMailToAdmin']);
}
/**
*@inheritdoc
*/
public static function tableName() {
return 'user';
}
/**
*@inheritdoc
*/
public function rules() {
return [
[[name', 'email'], 'string', 'max' => 255]
];
}
/**
*@inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
];
}
public function sendMailToAdmin($event) {
echo 'mail sent to admin using the event';
}
}
?>
上記のコードでは、「新規ユーザー」イベントを定義しています。 次に、init()メソッドで sendMailToAdmin 関数を「new-user」イベントに添付します。 次に、このイベントをトリガーする必要があります。
ステップ2 *-SiteControllerで *actionTestEvent というメソッドを作成します。
public function actionTestEvent() {
$model = new MyUser();
$model->name = "John";
$model->email = "[email protected]";
if($model->save()) {
$model->trigger(MyUser::EVENT_NEW_USER);
}
}
上記のコードでは、新しいユーザーを作成し、「new-user」イベントをトリガーします。
ステップ3 *- *http://localhost:8080/index.php?r = site/test-event と入力すると、次のように表示されます。