<?php
use yii\db\Migration;
class m160501_053640_add_column_to_admin extends Migration
{
public function up()
{
$this->addColumn('admin', 'status', $this->int(10)->nontNull());
}
public function down()
{
$this->dropColumn('admin', 'status');
}
}
} elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) {
$content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add_column']), [
'className' => $className,
'table' => mb_strtolower($matches[2], Yii::$app->charset),
'fields' => $this->fields
]);
}
return [ //修改migration模板 'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'templateFile'=>'@yii/views/migration.php',//默认模板,2.07后应该很少用了 'generatorTemplateFiles' => [ 'create_table' => '@console/views/createTableMigration.php',//修改的 'drop_table' => '@yii/views/dropTableMigration.php',//未修改的 'add_column' => '@console/views/addColumnMigration.php',//修改的 'drop_column' => '@console/views/dropColumnMigration.php',//修改的 'create_junction' => '@yii/views/createJunctionMigration.php'//未修改的 ], ], ], ];
<?php
/**
* This view is used by console/controllers/MigrateController.php
* The following variables are available in this view:
*/
/* @var $className string the new migration class name */
/* @var $table string the name table */
/* @var $fields array the fields */
echo "<?php\n";
?>
use yii\db\Migration;
class <?= $className ?> extends Migration
{
const TBL_NAME = '{{%<?=$table?>}}';
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="填写表注释"';
}
$this->createTable(self::TBL_NAME, [
<?php foreach ($fields as $field): ?>
<?php if ($field == end($fields)): ?>
'<?= $field['property'] ?>' => $this-><?= $field['decorators'].".\" COMMENT '填写段注释'\"" . "\n"?>
<?php else: ?>
'<?= $field['property'] ?>' => $this-><?= $field['decorators'].".\" COMMENT '填写段注释'\"" . ",\n"?>
<?php endif; ?>
<?php endforeach; ?>
],$tableOptions);
}
public function down()
{
$this->dropTable(self::TBL_NAME);
}
}
<?php
use yii\db\Migration;
class m160427_133556_create_admin extends Migration
{
const TBL_NAME = '{{%admin}}';
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="后台管理员表"';
}
$this->createTable(self::TBL_NAME, [
'id' => $this->primaryKey(),
'username'=>$this->string()->notNull()->unique()." COMMENT '用户名'",
'auth_key'=>$this->string(32)->notNull()." COMMENT '认证Key'",
'password_hash'=>$this->string()->notNull()." COMMENT '密码'",
'password_reset_token'=>$this->string()->unique()." COMMENT '密码重置Token'",
'email'=>$this->string()->notNull()->unique()." COMMENT '邮箱'",
'status'=>$this->smallInteger()->notNull()->defaultValue(10)." COMMENT '状态'",
'created_at' => $this->integer()->notNull()." COMMENT '创建时间'",
'updated_at' => $this->integer()->notNull()." COMMENT '更新时间'",
],$tableOptions);
}
public function down()
{
$this->dropTable(self::TBL_NAME);
}
}
<?php
/**
* Application initialization
* 参照深入理解Yii2.0视频教程
*/
namespace console\controllers;
use backend\models\Admin;
class InitController extends \yii\console\Controller
{
/**
* Create init admin
*/
public function actionAdmin()
{
echo "Create init admin ...\n"; // 提示当前操作
$username = $this->prompt('Admin Name:'); // 接收用户名
$email = $this->prompt('Email:'); // 接收Email
$password = $this->prompt('Password:'); // 接收密码
$model = new Admin(); // 创建一个新用户
$model->username = $username; // 完成赋值
$model->email = $email;
$model->password = $password;//注意这个地方,用了Admin模型中的setPassword方法(魔术方法__set)
if (!$model->save()) // 保存新的用户
{
foreach ($model->getErrors() as $error) // 如果保存失败,说明有错误,那就输出错误信息。
{
foreach ($error as $e)
{
echo "$e\n";
}
}
return 1; // 命令行返回1表示有异常
}
return 0; // 返回0表示一切OK
}
}
'components' => [ 'user' => [ 'identityClass' => 'backend\models\Admin', 'enableAutoLogin' => true, 'identityCookie' => [ 'name' => '_backendUser', // unique for backend ], ], 'session' => [ 'name' => 'PHPBACKSESSID', 'savePath' => sys_get_temp_dir(), ], 'request' => [ 'cookieValidationKey' => 'orGkZNZvZe3-4WicYHyGMS-EyI6Tp8yi',//random string 'csrfParam' => '_backendCSRF', ],
'components' => [ 'user' => [ 'identityClass' => 'frontend\models\User', 'enableAutoLogin' => true, 'identityCookie' => [ 'name' => '_frontendUser', // unique for frontend ] ], 'session' => [ 'name' => 'PHPFRONTSESSID', 'savePath' => sys_get_temp_dir(), ], 'request' => [ 'cookieValidationKey' => '8rqO22WJ9yiAx_KuJ8SFnbKctqGDWi9J', 'csrfParam' => '_frontendCSRF', ],
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有