Laravel 5.2.14与Duxet / RethinkDB播种错误

时间:2016-02-11 02:34:02

标签: laravel laravel-5 laravel-5.2

我为此项目设置了Homestead的每个项目安装,因此我不会将RethinkDB添加到我的所有Laravel应用程序中。我能够非常轻松地在Homestead中设置RethinkDB,并迁移数据库表,但我似乎无法为它们播种。运行php artisan db:seed会在终端中抛出此错误:

[Symfony\Component\Debug\Exception\FatalThrowableError]
Fatal error: Call to a member function prepare() on null

laravel.log中的错误:

[2016-02-11 02:15:39] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Call to a member function prepare() on null in /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:390
Stack trace:
#0 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(644): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(duxet\Rethinkdb\Connection), 'insert into "us...', Array)
#1 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(611): Illuminate\Database\Connection->runQueryCallback('insert into "us...', Array, Object(Closure))
#2 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(391): Illuminate\Database\Connection->run('insert into "us...', Array, Object(Closure))
#3 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(347): Illuminate\Database\Connection->statement('insert into "us...', Array)
#4 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\Database\Connection->insert('insert into "us...', Array)
#5 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1889): Illuminate\Database\Query\Processors\Processor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into "us...', Array, 'id')
#6 [internal function]: Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#7 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1244): call_user_func_array(Array, Array)
#8 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1683): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#9 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1652): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#10 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1543): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder), Array)
#11 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php(86): Illuminate\Database\Eloquent\Model->save()
#12 /home/vagrant/app/database/seeds/UserTableSeeder.php(35): Illuminate\Database\Eloquent\FactoryBuilder->create()
#13 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(39): UserTableSeeder->run()
#14 /home/vagrant/app/database/seeds/DatabaseSeeder.php(27): Illuminate\Database\Seeder->call('UserTableSeeder')
#15 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): DatabaseSeeder->run()
#16 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2347): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#17 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#18 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#19 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#20 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#21 /home/vagrant/app/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#22 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#23 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Console/Command.php(185): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#24 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(90): Illuminate\Console\Command->call('db:seed', Array)
#25 [internal function]: Illuminate\Database\Console\Migrations\MigrateCommand->fire()
#26 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#27 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#28 /home/vagrant/app/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#29 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#30 /home/vagrant/app/vendor/symfony/console/Application.php(791): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 /home/vagrant/app/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Migrations\MigrateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 /home/vagrant/app/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /home/vagrant/app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /home/vagrant/app/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

我的种子用户非常典型:

<?php

use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Developer accounts
        factory(App\User::class, 'admin', 1)->create([
            'username' => 'test',
            'email' => 'test@domain.com'
        ]);

        factory(App\User::class, 1)->create();
    }
}

使用简单的模型工厂:

<?php

/**
 * Define a general user factory for seeding the database.
 */
$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'username' => $faker->name,
        'password' => bcrypt(str_random(10)),
        'email' => $faker->email,
        'remember_token' => str_random(10),
    ];
});

/**
 * Define an administrator factory for seeding the database.
 */
$factory->defineAs(App\User::class, 'admin', function (Faker\Generator $faker) use ($factory) {

    // Reuse general user factory attributes
    $user = $factory->raw(App\User::class);

    return array_merge($user, [
        'role' => 'admin'
    ]);
});

更新

看起来它可能是RethinkDB和Laravel之间的问题,但我不确定如何修复它。这是问题发生的方法,我使用monolog注销到Laravel.log,并在日志语句下面包含输出。似乎方法getPdo()为null。

public function statement($ query,$ bindings = []) {     return $ this-&gt; run($ query,$ bindings,function($ me,$ query,$ bindings){         if($ me-&gt; pretending()){             返回true;         }

    Log::info($query);
    // insert into "users" ("username", "password", "email", "remember_token", "role", "updated_at", "created_at") values (?, ?, ?, ?, ?, ?, ?)

    $bindings = $me->prepareBindings($bindings);

    Log::info($bindings);
    // array (
    //   0 => 'test',
    //   1 => '$2y$10$8icEztzBbFL/556imvn8D.905i67RrCuUwf6csaN2r75W0s0ifg/a',
    //   2 => 'test@domain.com',
    //   3 => 'WUA1vvpyvQ',
    //   4 => 'admin',
    //   5 => '2016-02-11 03:33:29',
    //   6 => '2016-02-11 03:33:29',
    // )

    Log::info($me->getPdo());
    // null

    return $me->getPdo()->prepare($query)->execute($bindings);
});

}

1 个答案:

答案 0 :(得分:0)

由于用户模型是使用make:rethink-model通过工匠生成的唯一模型,因此看起来失败了,它引入了使用Illuminate Model类的Authenticatable。要解决此问题,必须手动更新它,方法是删除use Illuminate\Foundation\Auth\User as Authenticatable;,然后将其包含的特征直接应用于用户模型,然后继承自use \duxet\Rethinkdb\Eloquent\Model;,这样您的最终用户类现在看起来就像像这样:

namespace App;

use Illuminate\Auth\Authenticatable;
use \duxet\Rethinkdb\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;


class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}