問題描述
我正在 Laravel 4 中構(gòu)建一個(gè)包,但在嘗試訪問似乎是正確實(shí)例化對象的數(shù)據(jù)庫時(shí)出現(xiàn)非對象錯(cuò)誤.設(shè)置如下:
I'm building a package in Laravel 4 but am getting a non-object error when attempting to access the db from which seems to be a properly instantiated object. Here's the setup:
有問題的配置和類:
composer.json:
...
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"Vendor\Chat": "src/vendor/chat/src"
}
}
...
班級:
namespace VendorChat;
use IlluminateDatabaseEloquentModel as Eloquent;
class ChatHistory extends Eloquent
{
protected $table = 'chat_history';
protected $fillable = array('message', 'user_id', 'room_token');
public function __construct($attributes = array())
{
parent::__construct($attributes);
}
}
電話:
$message = new Message($msg);
$history = new ChatHistory;
$history->create(array(
'room_token' => $message->getRoomToken(),
'user_id' => $message->getUserId(),
'message' => $message->getMessage(),
));
錯(cuò)誤:
PHP Fatal error: Call to a member function connection() on a non-object in /home/vagrant/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2894
我相信我遺漏了一些基本的東西.感謝您的任何幫助!
I believe I'm missing something fundamental and under my nose. Thanks for any and all help!
這是實(shí)例化 ChatHistory 并調(diào)用寫入的類:
Here is the class that's instantiating ChatHistory and calling the write:
namespace VendorChat;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
use VendorChatClient;
use VendorChatMessage;
use VendorChatChatHistory;
use IlluminateDatabaseModel;
class Chat implements MessageComponentInterface {
protected $app;
protected $clients;
public function __construct()
{
$this->clients = new SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$client = new Client;
$client->setId($conn->resourceId);
$client->setSocket($conn);
$this->clients->attach($client);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
$message = new Message($msg);
$history = new ChatHistory;
ChatHistory::create(array(
'room_token' => $message->getRoomToken(),
'user_id' => $message->getUserId(),
'message' => $message->getMessage(),
));
/* error here */
/* ... */
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, Exception $e)
{
$conn->close();
}
protected function getClientByConn(ConnectionInterface $conn)
{
foreach($this->clients as $client) {
if($client->getSocket() === $conn) {
return $client;
}
}
return null;
}
}
DB 不可用的事實(shí)表明 Eloquent 沒有被加載到頂部?
The fact that DB isn't available suggest that Eloquent isn't being loaded up top?
推薦答案
答案:
在您的服務(wù)提供商的 boot
方法中引導(dǎo)您的包.
Bootstrap your package in your service provider's boot
method.
說明:
既然你正在開發(fā)一個(gè)與 Laravel 一起使用的包,那么制作你自己的 Capsule
實(shí)例是沒有意義的.你可以直接使用 Eloquent
.
Since you're developing a package to be used with Laravel, there's no point in making your own Capsule
instance. You can just use Eloquent
directly.
您的問題似乎源于 DB
/Eloquent
在您的代碼命中時(shí)尚未設(shè)置.
Your problem seems to stem from DB
/Eloquent
not being set up yet by the time your code hits it.
您沒有向我們展示您的服務(wù)提供商,但我猜您正在使用一個(gè)并在 register
方法中完成所有操作.
You have not shown us your service provider, but I'm guessing you're using one and doing it all in the register
method.
由于您的包依賴于不同的服務(wù)提供商 (DatabaseServiceProvider
) 在它自己執(zhí)行之前進(jìn)行連接,因此引導(dǎo)包的正確位置是您的服務(wù)提供商的 boot代碼>方法.
Since your package depends on a different service provider (DatabaseServiceProvider
) to be wired up prior to its own execution, the correct place to bootstrap your package is in your service provider's boot
method.
這里引用了 文檔:
register
方法在服務(wù)提供者注冊后立即調(diào)用,而 boot
命令僅在請求被路由之前調(diào)用.
The
register
method is called immediately when the service provider is registered, while theboot
command is only called right before a request is routed.
因此,如果您的服務(wù)提供者中的操作依賴于已經(jīng)注冊的另一個(gè)服務(wù)提供者 [...],您應(yīng)該使用 boot
方法.
So, if actions in your service provider rely on another service provider already being registered [...] you should use the boot
method.
這篇關(guān)于Laravel/Eloquent:致命錯(cuò)誤:在非對象上調(diào)用成員函數(shù) connection()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!