【问题标题】:Laravel 4 seed returns successful but nothing in databaseLaravel 4种子返回成功但数据库中没有
【发布时间】:2013-10-26 06:02:48
【问题描述】:

我正在使用 Laravel 4 并尝试使用一些用户为数据库播种。我正在使用Zizaco Confide 插件,所以我的User 模型根据文档扩展ConfideUser 而不是Eloquent。我还在模型中添加了一些字段,但没有什么超级复杂的。我已经尝试删除这些字段,但我遇到了同样的问题。

我创建了一个UserSeeder 类,我用DatabaseSeeder 调用它,然后运行php artisan migrate:refresh --seed。它运行没有错误并返回“数据库已播种”,除users 之外的每个表都是如此。没有用户被插入。我尝试使用User::create(array(...)) 和$user = new User ... $user->save() 创建用户,我得到了相同的结果。没有错误被转储,并且我在系统上可以找到的任何日志中都没有任何内容。如果我在 UserSeeder->run() 方法中插入一些 var_dump,我会看到这些对象是使用正确的值创建的,但没有任何保存。

我错过了什么?这是一些代码示例,如果需要,我可以提供更多:

模型\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' => 'nathan@abc.com',
                '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:: 插入,因为它有特殊的验证。

标签: php mysql laravel-4 eloquent


【解决方案1】:

在您的用户播种器类中执行此操作:

class UsersTableSeeder extends Seeder
{
    public function run()
    {

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



        $users = array(
        array(  'username' => 'local_test',
            'email' => 'nathan@abc.com',
            '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');
    }

}

【讨论】:

  • 由于某些外键引用,truncate() 出现错误,但如果我将其保留为 delete() 并使用您建议的数组方法,它就可以工作。你能解释一下为什么这行得通而User::create() 和$user = new User ... $user-&gt;save() 不行吗?
  • 我真的不知道..我以前用过这个,我知道它会起作用..但即使在文档中,示例也是按照您指出的方式编写的:laravel.com/docs/migrations#database-seeding ..我想知道如果您只是扩展 Eloquent 而不是 ConfideUser 是否会起作用
  • 是的!!!!谢谢!我应该知道是 Confide 做的特殊验证阻止了我的种子功能。
【解决方案2】:

Confide 使用 Ardent 进行验证。需要添加“password_confirmation”属性。此外,您不需要 Hash::make,因为 Confide 也会为您处理。

<?php

class UsersTableSeeder extends Seeder
{
    public function run()
    {

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

        $users = array(
        array(  'username' => 'local_test',
            'email' => 'nathan@abc.com',
            '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);

    }
}

【讨论】:

    【解决方案3】:

    我发现播种机无声无息地失败了。

    我看到了一个用户表。回报是种子的成功,但它是空的。 我使用插入方法遵循了上面的答案。 执行上述方法时,返回错误,说数据库表中某列没有默认值。

    我调整了列并从 laravel 文档恢复为 create 方法。 然后就成功了。

    所以在我的例子中,有一个数据库错误,但它没有告诉我就失败了。 使用插入方法,我能够找到错误。 然后 create 方法在没有错误的情况下工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2012-03-26
      • 2018-01-19
      • 2020-10-14
      相关资源
      最近更新 更多