wave
นิมิต นิยม
2,173
สังคมนักพัฒนาระบบ (Community)
มือใหม่ครับ

<p>&gt;&gt;&gt;รบกวนอาจารย์ช่วยดูโค้ดให้หน่อยครับว่าผิดตรงไหนบ้างครับ.....ไม่ create table เลยครับ ทำตามอาจารย์ทุกอย่างแล้วแต่ในฐานข้อมูลไม่มีการ Create Table เลย....<span style="background-color: initial;">มือใหม่หัดเล่นครับ&lt;&lt;&lt;</span></p><p>&lt;?php</p><p>use yiidbSchema;</p><p>use yiidbMigration;</p><p>class m150724_021725_create_user_profile_table extends Migration</p><p>{</p><p> public function up() {</p><p>$this-&gt;createTable('user', [</p><p> 'id' =&gt; Schema::TYPE_PK,</p><p> 'username' =&gt; Schema::TYPE_STRING.'NOT NULL',</p><p> 'auth_key' =&gt; Schema::TYPE_STRING,</p><p> 'password_hash' =&gt; Schema::TYPE_STRING.'NOT NULL',</p><p> 'password_reset_token' =&gt; Schema::TYPE_STRING,</p><p> 'email' =&gt; Schema::TYPE_STRING.'NOT NULL',</p><p> 'role' =&gt; Schema::TYPE_SMALLINT,</p><p> 'status' =&gt; Schema::TYPE_SMALLINT,</p><p> 'created_at' =&gt; Schema::TYPE_TIMESTAMP,</p><p> 'updated_at' =&gt; Schema::TYPE_TIMESTAMP,</p><p> ]);</p><p> $this-&gt;createIndex('username', 'user', 'username', true);</p><p> $this-&gt;createIndex('email', 'user', 'email', true);</p><p> $this-&gt;createTable('profile', [</p><p> 'id' =&gt; Schema::TYPE_PK,</p><p> 'user_id' =&gt; Schema::TYPE_INTEGER.'NOT NUL',</p><p> 'fullname' =&gt; Schema::TYPE_STRING.'NOT NULL',</p><p> 'created_at' =&gt; Schema::TYPE_TIMESTAMP, </p><p> ]);</p><p> $security = yii::$app-&gt;security;</p><p> $this-&gt;batchInsert('user', ['username', 'auth_key', 'password_hash','email','role','status','created_at'],[</p><p> ['admin',$security-&gt;generateRandomString(),$security-&gt;generatePasswordHash('admin'),'admin@admin.com',10,1,date("Y-m-d H:i:s")],</p><p> ['demo',$security-&gt;generateRandomString(),$security-&gt;generatePasswordHash('demo'),'demo@demo.com',1,1,date("Y-m-d H:i:s")],</p><p> ]);</p><p> $this-&gt;batchInsert('profile', ['user_id','fullname','created_at'], [</p><p> [1,'Administrator',date("Y-m-d H:i:s")],</p><p> [2,'Demostration',date("Y-m-d H:i:s")],</p><p> ]);</p><p> }</p><p> public function down() {</p><p> $this-&gt;dropTable('user');</p><p> $this-&gt;dropTable('profile');</p><p> }</p><p> /*</p><p> // Use safeUp/safeDown to run migration code within a transaction</p><p> public function safeUp()</p><p> {</p><p> }</p><p> public function safeDown()</p><p> {</p><p> }</p><p> */</p><p>}</p>

2 ความคิดเห็น

  • มานพ กองอุ่น

  • นิมิต นิยม

>>>รบกวนอาจารย์ช่วยดูโค้ดให้หน่อยครับว่าผิดตรงไหนบ้างครับ.....ไม่ create table เลยครับ ทำตามอาจารย์ทุกอย่างแล้วแต่ในฐานข้อมูลไม่มีการ Create Table เลย....มือใหม่หัดเล่นครับ<<<

<?php

use yii\db\Schema;

use yii\db\Migration;

class m150724_021725_create_user_profile_table extends Migration

{

public function up() {

$this->createTable('user', [

'id' => Schema::TYPE_PK,

'username' => Schema::TYPE_STRING.'NOT NULL',

'auth_key' => Schema::TYPE_STRING,

'password_hash' => Schema::TYPE_STRING.'NOT NULL',

'password_reset_token' => Schema::TYPE_STRING,

'email' => Schema::TYPE_STRING.'NOT NULL',

'role' => Schema::TYPE_SMALLINT,

'status' => Schema::TYPE_SMALLINT,

'created_at' => Schema::TYPE_TIMESTAMP,

'updated_at' => Schema::TYPE_TIMESTAMP,

]);

$this->createIndex('username', 'user', 'username', true);

$this->createIndex('email', 'user', 'email', true);

$this->createTable('profile', [

'id' => Schema::TYPE_PK,

'user_id' => Schema::TYPE_INTEGER.'NOT NUL',

'fullname' => Schema::TYPE_STRING.'NOT NULL',

'created_at' => Schema::TYPE_TIMESTAMP,

]);

$security = yii::$app->security;

$this->batchInsert('user', ['username', 'auth_key', 'password_hash','email','role','status','created_at'],[

['admin',$security->generateRandomString(),$security->generatePasswordHash('admin'),'admin@admin.com',10,1,date("Y-m-d H:i:s")],

['demo',$security->generateRandomString(),$security->generatePasswordHash('demo'),'demo@demo.com',1,1,date("Y-m-d H:i:s")],

]);

$this->batchInsert('profile', ['user_id','fullname','created_at'], [

[1,'Administrator',date("Y-m-d H:i:s")],

[2,'Demostration',date("Y-m-d H:i:s")],

]);

}

public function down() {

$this->dropTable('user');

$this->dropTable('profile');

}

/*

// Use safeUp/safeDown to run migration code within a transaction

public function safeUp()

{

}

public function safeDown()

{

}

*/

}

ไม่ทราบว่าได้มีการเชื่อมต่อฐานข้อมูลหรือยังครับใน config/main-local.php