我正在使用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
)
);
使用Confide時,需要使用DB ::插入,因爲它具有特殊的驗證。 – malhal