Yii - Etkinlik Oluşturma
Bu bölümde Yii'de bir etkinlik oluşturmayı göreceğiz. Olayları iş başında göstermek için verilere ihtiyacımız var.
DB'yi hazırlama
Step 1- Yeni bir veritabanı oluşturun. Veritabanı aşağıdaki iki şekilde hazırlanabilir.
Terminalde mysql -u root –p komutunu çalıştırın
CREATE DATABASE ile yeni bir veritabanı oluşturun helloworld CHARACTER SET utf8 COLLATE utf8_general_ci;
Step 2 - Veri tabanı bağlantısını yapılandırın. config/db.phpdosya. Aşağıdaki konfigürasyon şu anda kullanılan sistem içindir.
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=helloworld',
'username' => 'vladimir',
'password' => '12345',
'charset' => 'utf8',
];
?>
Step 3 - Kök klasörün içinde run ./yii migrate/create test_table. Bu komut, DB'mizi yönetmek için bir veritabanı geçişi yaratacaktır. Taşıma dosyası,migrations proje kök klasörü.
Step 4 - Taşıma dosyasını değiştirin (m160106_163154_test_table.php bu durumda) bu şekilde.
<?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');
}
}
?>
Yukarıdaki geçiş, bir userBu alanların bulunduğu tablo: id, ad ve e-posta. Ayrıca birkaç demo kullanıcısı ekler.
Step 5 - Proje kökünün içinde run ./yii migrate veri tabanına geçişi uygulamak için.
Step 6 - Şimdi, bizim için bir model oluşturmamız gerekiyor. usertablo. Basitlik uğruna, kullanacağızGiikod oluşturma aracı. Aç şunuurl: http://localhost:8080/index.php?r=gii. Ardından, "Model oluşturucu" başlığının altındaki "Başlat" düğmesine tıklayın. Tablo Adını ("kullanıcı") ve Model Sınıfını ("MyUser") doldurun, "Önizleme" düğmesini tıklayın ve son olarak "Oluştur" düğmesini tıklayın.

MyUser modeli modeller dizininde görünmelidir.
Bir Etkinlik Oluşturun
Web sitemize yeni bir kullanıcı kaydolduğunda yöneticiye bir e-posta göndermek istediğimizi varsayalım.
Step 1 - Değiştirin models/MyUser.php bu şekilde dosyalayın.
<?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';
}
}
?>
Yukarıdaki kodda bir "yeni kullanıcı" olayı tanımlıyoruz. Daha sonra init () yöntemindesendMailToAdmin"yeni kullanıcı" olayı işlevi. Şimdi bu olayı tetiklememiz gerekiyor.
Step 2 - adlı bir yöntem oluşturun actionTestEvent SiteController'da.
public function actionTestEvent() {
$model = new MyUser();
$model->name = "John"; $model->email = "[email protected]";
if($model->save()) { $model->trigger(MyUser::EVENT_NEW_USER);
}
}
Yukarıdaki kodda yeni bir kullanıcı oluşturup “yeni kullanıcı” olayını tetikliyoruz.
Step 3 - Şimdi yazın http://localhost:8080/index.php?r=site/test-event, aşağıdakileri göreceksiniz.
