1

I have installed Sentry with Composer, thus it has installed Illuminate Database too. The installation was successful, I have done everything in the Sentry documentation. I'm trying to add a user to the database with a simple code. However it gives me this error message:

Fatal error: Call to a member function connection() on a non-object in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\ilhan\vendor\illuminate\database\Illuminate\Database\Eloquent\Model.php on line 2472

My code is as follows:

<?php
include_once "vendor/autoload.php";


use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'ilhantestdb',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
]);



use Cartalyst\Sentry\Sentry as Sentry;


try
{
    $user = new Sentry;
    // Create the user
    $user->createUser(array(
        'email'     => 'john.doe@example.com',
        'password'  => 'test',
        'activated' => true,
    ));

    // Find the group using the group id
    $adminGroup = Sentry::findGroupById(1);

    // Assign the group to the user
    $user->addGroup($adminGroup);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
    echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
    echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
    echo 'User with this login already exists.';
}
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
{
    echo 'Group was not found.';
}

Even I don't know how to debug this. Also, I thought that since Illuminate comes with Sentry, Sentry should be coded how to handle Illuminate thus I don't need much configuration. The documentation is poor and I was unable to find what to do with this error.

4

2 回答 2

4

您还必须引导 ORM。文档对此并不十分清楚。

use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
     ...
]);

$capsule->bootEloquent();
于 2014-03-02T14:42:10.447 回答
1

尝试将 Capsule 设置为全局。我无法与 Sentry 交谈,但我自己正在尝试在一个项目中使用 Capsule,并且遇到了同样的问题。

看看这里:https ://github.com/illuminate/database/blob/master/Capsule/Manager.php#L113你会看到那里的很多便利功能都依赖于设置static::$instance变量,它得到在https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L192上设置。

就我而言,我正在尝试使用 $capsule 而不将其设置为全局。我最终需要做的是像这样写我的查询$capsule->getConnection()->table('foo')->get()。在您的情况下,我认为 Sentry 正在尝试通过静态类方法访问 Eloquent 和 Capsule。

tl;博士运行$capsule->setAsGlobal();

于 2014-07-03T18:37:05.723 回答