Skip to content

Latest commit

 

History

History
92 lines (76 loc) · 2.05 KB

gii-generator.md

File metadata and controls

92 lines (76 loc) · 2.05 KB

Gii generator

You can generate email templates with Gii!

For this you should configure generator in your application config like the following:

If you use advanced application template put this code to backend/config/main-local.php or frontend/config/main-local.php configuration file

'modules' => [
    'gii' => [
        // ...
        'generators' => [
            // ...
            'email-templates' => ymaker\email\templates\gii\Generator::class,
        ],
    ],
],

Generator creates migration for inserting an email template to database table.

<?php

use yii\db\Migration;
use ymaker\email\templates\entities\EmailTemplate;

/**
 * Handles creating of email template.
 *
 * Generated by `yiimaker/yii2-email-templates`.
 * @see https://github.com/yiimaker/yii2-email-templates
 */
class m180121_180103_add_email_template extends Migration
{
    /**
     * Migration table name.
     *
     * @var string
     */
    public $tableName = '{{%email_template}}';
    /**
     * Migration table name.
     *
     * @var string
     */
    public $translationTableName = '{{%email_template_translation}}';


    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $this->insert($this->tableName, [
            'key' => 'preview',
        ]);

        $templateId = EmailTemplate::find()
            ->select('id')
            ->where(['key' => 'preview'])
            ->scalar();

        $this->insert($this->translationTableName, [
            'templateId'    => $templateId,
            'language'      => Yii::$app->language,
            'subject'       => 'Hello',
            'body'          => 'Hello, {username}!',
            'hint'          => 'This is hint',
        ]);
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        $this->delete($this->tableName, '[[key]] = :key', [
            ':key' => 'preview',
        ]);
    }
}

After generation you should run ./yii migrate command in console.

Generator preview

yii2 email templates