2013-10-17 113 views
6

我正在使用Laravel 4並試圖與一些用戶一起種子數據庫。我正在使用Zizaco Confide插件,所以我的User型號擴展了ConfideUser,而不是每個文檔的Eloquent。我也爲模型添加了幾個字段,但沒有超級複雜。我試圖刪除這些領域,但我得到同樣的問題。Laravel 4種子返回成功,但數據庫中沒有任何數據

我創建了一個UserSeeder類,我用DatabaseSeeder調用,然後運行php artisan migrate:refresh --seed。它運行時沒有錯誤,並返回「數據庫已播種」,對於除users以外的每個表均如此。沒有用戶被插入。我試着用User::create(array(...))以及$user = new User ... $user->save()創建用戶,我得到了相同的結果。沒有錯誤被傾倒,並且沒有任何日誌可以在系統上找到。如果我在UserSeeder-> run()方法中插入了一些var_dumps,我發現這些對象是用正確的值創建的,但沒有保存。

我錯過了什麼?下面是一些代碼樣本,如果需要,我可以提供更多:

型號\ user.php的:

<?php 

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 
use Zizaco\Confide\ConfideUser; 

//class User extends Eloquent implements UserInterface, RemindableInterface { 
class User extends ConfideUser 
{ 
    // for Entrust 
    use \Zizaco\Entrust\HasRole; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 

    public function agency() 
    { 
     if ($this->agency_type == 'local') 
     { 
      return $this->hasOne('Local'); 
     } 

     if ($this->agency_type == 'county') 
     { 
      return $this->hasOne('County'); 
     } 
    } 

    /** 
    * Get the unique identifier for the user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->getKey(); 
    } 

    /** 
    * Get the password for the user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the e-mail address where password reminders are sent. 
    * 
    * @return string 
    */ 
    public function getReminderEmail() 
    { 
     return $this->email; 
    } 

} 

數據庫\遷移\ xxxxxxxxx_confide_setup_users_table.php:

<?php 
use Illuminate\Database\Migrations\Migration; 

class ConfideSetupUsersTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     // Creates the users table 
     Schema::create('users', function($table) 
     { 
      $table->increments('id'); 
      $table->string('username'); 
      $table->string('email'); 
      $table->string('password'); 
      $table->string('confirmation_code'); 
      $table->boolean('confirmed')->default(false); 
      $table->string('address1'); 
      $table->string('address2')->nullable(); 
      $table->string('state', 2); 
      $table->string('zipcode', 9); 
      $table->string('phone', 10); 
      $table->string('extension',5)->nullable(); 
      $table->string('fax', 10)->nullable(); 
      $table->enum('agency_type', array('local', 'county', 'state'))->default('local'); 
      $table->integer('agency')->unsigned()->nullable(); 
      $table->dateTime('last_seen'); 
      $table->timestamps(); 
      $table->softDeletes(); 
     }); 

     // Creates password reminders table 
     Schema::create('password_reminders', function($t) 
     { 
      $t->string('email'); 
      $t->string('token'); 
      $t->timestamp('created_at'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('password_reminders'); 
     Schema::drop('users'); 
    } 

} 

數據庫\種子\ UserSeeder。 php:

<?php 

use \Illuminate\Database\Seeder; 

class UserSeeder extends Seeder 
{ 
    public function run() 
    { 
     DB::table('users')->delete(); 

     User::create(
      array(
       'username' => 'local_test', 
       'email' => '[email protected]', 
       'password' => Hash::make('local'), 
       'confirmation_code' => '483JU3ID8', 
       'confirmed' => true, 
       'address1' => '123 Main St.', 
       'state' => 'MI', 
       'zipcode' => '12345', 
       'phone' => '5559993436', 
       'agency_type' => 'local', 
       'agency' => null, 
       'last_seen' => new DateTime 
      ) 
     ); 
+0

使用Confide時,需要使用DB ::插入,因爲它具有特殊的驗證。 – malhal

回答

3

在您的用戶播種機類中執行此操作:

class UsersTableSeeder extends Seeder 
{ 
    public function run() 
    { 

    DB::table('users')->truncate(); 



     $users = array(
     array( 'username' => 'local_test', 
      'email' => '[email protected]', 
      'password' => Hash::make('local'), 
      'confirmation_code' => '483JU3ID8', 
      'confirmed' => true, 
      'address1' => '123 Main St.', 
      'state' => 'MI', 
      'zipcode' => '12345', 
      'phone' => '5559993436', 
      'agency_type' => 'local', 
      'agency' => null, 
      'last_seen' => new DateTime 
       ) 
     ); 

      // make sure you do the insert 
     DB::table('users')->insert($users); 

} 
} 

,然後,請確保您調用它在你的DatabaseSeeder.php文件

<?php 

class DatabaseSeeder extends Seeder { 

    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    {   
     $this->call('UsersTableSeeder'); 
    } 

} 
+0

由於某些外鍵引用,我得到了'truncate()'的錯誤,但是如果我將它保留爲'delete()'並使用你建議的數組方法,那麼它就可以工作。你能解釋一下爲什麼這樣,'User :: create()'和'$ user = new User ... $ user-> save()'不? –

+0

我真的不知道..我以前用過這個,我知道它會工作..但即使在文檔中,示例也是按照您指出的方式編寫的:http://laravel.com/docs/migrations#database-seeding ..我想知道如果您只是擴展了'Eloquent'而不是' ConfideUser' –

+0

是的!謝謝!我應該知道這是Confide做了阻止我種子功能的特殊驗證。 –

1

傾訴使用殷切進行驗證。需要添加'password_confirmation'屬性。你也不需要Hash :: make,因爲Confide也會爲你處理。

<?php 

class UsersTableSeeder extends Seeder 
{ 
    public function run() 
    { 

     DB::table('users')->truncate(); 

     $users = array(
     array( 'username' => 'local_test', 
      'email' => '[email protected]', 
      'password' => 'local', 
      'password_confirmation' => 'local', 
      'confirmation_code' => '483JU3ID8', 
      'confirmed' => true, 
      'address1' => '123 Main St.', 
      'state' => 'MI', 
      'zipcode' => '12345', 
      'phone' => '5559993436', 
      'agency_type' => 'local', 
      'agency' => null, 
      'last_seen' => new DateTime 
       ) 
     ); 

     // make sure you do the insert 
     DB::table('users')->insert($users); 

    } 
} 
0

我發現播種機默默無聞。

我看到一個用戶表。迴歸是種子成功,但它是空的。 我遵循上面的使用插入方法的答案。 執行上述方法時,會返回一個錯誤,指出列在數據庫表中沒有缺省值。

我調整了列並從laravel文檔恢復爲create方法。 它是成功的。

所以在我的例子中有一個數據庫錯誤,但沒有告訴我失敗。 使用插入方法,我能夠找到錯誤。 然後,創建方法一旦沒有錯誤就可以工作。

相關問題