2014-02-18 52 views
1

我已將Sentry與Composer一起安裝,因此它也安裝了Illuminate Database。安裝成功了,我已經完成了Sentry文檔中的所有工作。我試圖用簡單的代碼將用戶添加到數據庫中。然而,它給了我此錯誤消息:在使用Illuminate Database時調用非對象的成員函數連接()時出錯

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 

我的代碼如下:

<?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'  => '[email protected]', 
     '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.'; 
} 

即使我不知道如何調試這一點。另外,我認爲自Illuminate自帶Sentry以來,Sentry應該被編碼爲如何處理Illuminate,因此我不需要太多配置。文檔很差,我無法找到如何處理這個錯誤。

回答

4

您還必須啓動ORM。文檔不是很清楚。

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

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

$capsule->bootEloquent(); 
+1

我花了幾個小時在這!這解決了我的問題,謝謝! –

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()。在你的情況下,我認爲哨兵試圖通過靜態類方法訪問Eloquent和Capsule。

tl; dr Run $capsule->setAsGlobal();

相關問題