使用Laravel的Eloquent ORM调用Slim中的null错误的成员函数connection()

时间:2016-07-07 23:28:50

标签: laravel orm eloquent slim

我正在尝试在Slim微框架上使用Laravel的Eloquent ORM,但我一直在看错:在null上调用成员函数connection()

以下是代码:

dependency.php

$container['db'] = function($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container->get('settings')['database']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

User.php(模型类)

use Illuminate\Database\Eloquent\Model as Model;

class User extends Model {

    protected $table = "users";

    protected $fillable = ['name', 'email', 'password'];
}

HomeController.php(控制器类)

class Home extends Controller {

public function index($request, $response, $args) {
        $user = User::find(1);
        var_dump($user);
        die();
        $title = "Slim Auth";
        $response = $this->view->render($response, 'home.php', ["title" => $title]);
        return $response;
    }
}

在我的bootstrap.php文件中需要dependency.php,其中实例化了Slim类:

$config = [
    'settings' => [
        'displayErrorDetails' => true,

        'view' => [
            'view_path' => APP_PATH . 'views/'
        ],

        'database' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'tutorial_slim_auth',
            'username'  => 'root',
            'password'  => 'passw0rd',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ],
    ]
];

$app = new Slim\App($config);

而HomeController.php和User.php是通过composer json文件自动加载的。运行index.php(也包括我的bootstrap.php)文件后,包含:

$app->run();

这给了我一个致命的错误:在null上调用成员函数connection()。 但这样做:

echo '<pre>';
print_r($container['db']);
echo '</pre>';
我的bootstrap文件中的

产生了预期的结果以及HomeController.php文件中调用的var_dump函数的结果。我该怎么办?或者有什么我做不对的吗?

2 个答案:

答案 0 :(得分:13)

你的全球$ capsule封装在这里:

$container['db'] = function($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container->get('settings')['database']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

试试这个:

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container['db'] = function ($container) use ($capsule) {
    return $capsule;
};

第一个版本不起作用的原因是只有在您实际使用胶囊时才会调用该函数,即$this->db。如果您只使用Eloquent模型类,则不会进行任何调用。

答案 1 :(得分:0)

在多重连接上,试试这个:

$dbconfig = $ci->settings['db'];

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

foreach ($dbconfig['connections'] as $cname => $connection)
{
    $cname = $cname == $dbconfig['default'] ? 'default' : $cname;

    $capsule->addConnection($connection, $cname);
}

$capsule->setFetchMode($dbconfig['fetch']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$ci['db'] = function ($ci) use($capsule) {
    return $capsule;
};

然后

DB::connection('other_connection')->table('...')->get();