Laravel / Eloquent:致命错误:调用非对象上的成员函数connection() [英] Laravel/Eloquent: Fatal error: Call to a member function connection() on a non-object

查看:2594
本文介绍了Laravel / Eloquent:致命错误:调用非对象上的成员函数connection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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
}
}
...

strong>类:

 命名空间Vendor \Chat; 

使用Illuminate \Database\Eloquent\Model作为口才;


class ChatHistory extends Eloquent
{
protected $ table ='chat_history';

protected $ fillable = array('message','user_id','room_token');

public function __construct($ attributes = array())
{
parent :: __ construct($ attributes);
}

}

/ strong>

  $ message = new Message($ msg); 

$ history = new ChatHistory;
$ history-> create(array(
'room_token'=> $ message-> getRoomToken(),
'user_id'=> $ message-> getUserId ,
'message'=> $ message-> getMessage(),
));

错误:

  PHP致命错误:调用/ home / vagrant / project / vendor / laravel / framework / src / Illuminate / Database中的非对象上的成员函数connection Eloquent / Model.php on line 2894 

我相信我遗漏了一些基本的东西,感谢任何和所有的帮助!



编辑:



下面是实例化ChatHistory并调用write :

 命名空间Vendor \Chat; 

使用Ratchet\ MessageComponentInterface;
使用Ratchet\ConnectionInterface;

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

使用Illuminate \Database\Model;

class Chat实现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


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






说明



由于您正在开发一个与Laravel一起使用的软件包, Capsule 实例。您可以直接使用 Eloquent



您的问题似乎源于 code> / Eloquent 尚未到您的代码到达时才设置。



没有向我们显示您的服务提供商,但我猜你正在使用一个,并在注册方法。



由于您的包取决于在自己执行之前连接的不同服务提供程序( DatabaseServiceProvider ),所以在您的服务中引导包的正确位置
$ b /4.2/packages#service-providers\">文档:


注册表方法在服务提供程序注册时立即被调用,而 boot 命令只在请求被路由之前调用。



因此,如果你的服务提供者的操作依赖于已经注册的另一个服务提供者[...],你应该使用 boot p>


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:

The config and class in question:

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"
        }  
    }
...

The class:

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);
    }

}

The call:

$message = new Message($msg);

$history = new ChatHistory;
$history->create(array(
                 'room_token' => $message->getRoomToken(),
                 'user_id' => $message->getUserId(),
                 'message' => $message->getMessage(),
              ));

The error:

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!

EDIT:

Here is the class that's instantiating ChatHistory and calling the 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;
    }
}

The fact that DB isn't available suggest that Eloquent isn't being loaded up top?

解决方案

Answer:

Bootstrap your package in your service provider's boot method.


Explanation:

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.

Your problem seems to stem from DB/Eloquent not being set up yet by the time your code hits it.

You have not shown us your service provider, but I'm guessing you're using one and doing it all in the register method.

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.

Here's a quote from 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.

这篇关于Laravel / Eloquent:致命错误:调用非对象上的成员函数connection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆