Yii 创建动作
在本章中,我们将看到在Yii中创建一个事件。为了显示活动中的事件,我们需要数据。
准备数据库
第1步 - 创建一个新的数据库。 数据库可以通过以下两种方式进行准备。
- 在终端中运行 mysql -u root -p
- 通过 CREATE DATABASE 创建一个新的数据库 _helloworld CHARACTER SET utf8 COLLATE utf8_generalci;
第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 。该命令将创建用于管理我们数据库的数据库迁移。迁移文件应该出现在项目根目录的 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", "user1@gmail.com"],
["User2", "user2@gmail.com"],
["User3", "user3@gmail.com"],
["User4", "user4@gmail.com"],
["User5", "user5@gmail.com"],
["User6", "user6@gmail.com"],
["User7", "user7@gmail.com"],
["User8", "user8@gmail.com"],
["User9", "user9@gmail.com"],
["User10", "user10@gmail.com"],
["User11", "user11@gmail.com"],
]);
}
public function safeDown() {
$this->dropTable('user');
}
}
?>
上述迁移创建一个包含这些字段的 用户 表:id,名称和电子邮件。它还增加了一些演示用户。
第5步 - 在项目根目录 运行./yii migrate 以将 迁移 应用到数据库。
第6步 - 现在,我们需要为我们的 用户 表创建一个模型。为了简单起见,我们将使用 Gii 代码生成工具。打开这个 URL:http:// localhost:8080 / index.php?r = gii 。然后,点击“模型生成器”标题下的“开始”按钮。填写表名(“用户”)和模型类(“MyUser”),点击“预览”按钮,最后点击“生成”按钮。

MyUser模型应该出现在模型目录中。
创建一个事件
假设我们希望在新用户在我们的网站上注册时向管理员发送电子邮件。
第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 函数附加到“新用户”事件。现在,我们需要触发这个事件。
第2步 - 在 SiteController 中创建一个名为 actionTestEvent 的方法。
public function actionTestEvent() {
$model = new MyUser();
$model->name = "John";
$model->email = "john@gmail.com";
if($model->save()) {
$model->trigger(MyUser::EVENT_NEW_USER);
}
}
在上面的代码中,我们创建一个新用户并触发“新用户”事件。
第3步 - 现在键入 http:// localhost:8080 / index.php?r =站点/测试事件 ,您将看到以下内容。

下一章:Yii 行为
行为是yii \ base \ Behavior类的实例。行为将其方法和属性注入到它所连接的组件。行为也可以响应组件触发的事件。第1步 - 要定义行为,请扩展 yii \ base \ Behavior 类。namespace ...
AI 中文社