gpt4 book ai didi

php - Laravel/ Eloquent : Fatal error: Call to a member function connection() on a non-object

转载 作者:IT王子 更新时间:2023-10-29 00:07:03 27 4
gpt4 key购买 nike

我正在 Laravel 4 中构建一个包,但在尝试访问似乎是正确实例化对象的数据库时遇到非对象错误。这是设置:

有问题的配置和类:

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 Vendor\Chat;

use Illuminate\Database\Eloquent\Model 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(),
));

错误:

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

我相信我在我的眼皮底下遗漏了一些基本的东西。感谢您提供的所有帮助!

编辑:

这是实例化 ChatHistory 并调用 write 的类:

namespace Vendor\Chat;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

use Vendor\Chat\Client;
use Vendor\Chat\Message;
use Vendor\Chat\ChatHistory;

use Illuminate\Database\Model;

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 不可用的事实表明 Eloquent 没有被加载到顶部吗?

最佳答案

回答:

在服务提供商的 boot 方法中引导您的包。


解释:

既然你正在开发一个与 Laravel 一起使用的包,那么创建你自己的 Capsule 实例是没有意义的。您可以直接使用 Eloquent

您的问题似乎源于 DB/Eloquent 在您的代码命中时尚未设置。

您没有向我们展示您的服务提供商,但我猜您正在使用一个并在 register 方法中完成这一切。

由于您的包依赖于不同的服务提供商 (DatabaseServiceProvider) 在其自身执行之前进行连接,因此引导您的包的正确位置是在您的服务提供商的 boot 方法。

这是来自 the docs 的引述:

The register method is called immediately when the service provider is registered, while the boot command is only called right before a request is routed.

So, if actions in your service provider rely on another service provider already being registered [...] you should use the boot method.

关于php - Laravel/ Eloquent : Fatal error: Call to a member function connection() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com