【发布时间】:2014-12-28 03:07:39
【问题描述】:
我正在 Laravel 4 中构建一个包,但在尝试访问似乎是正确实例化对象的 db 时出现非对象错误。这是设置:
有问题的配置和类:
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 没有被加载到顶部?
【问题讨论】:
-
您在哪一行收到此错误?是为了历史还是使用
$message的方法。我们当然不知道,也不知道Message是什么 -
道歉。在 ChatHistory 上调用 create() 时会触发该错误。 Message 是一个表示套接字消息的简单类。
-
你是否尝试过像laracasts.com/forum/?p=969-codeception-and-laravel/0 中描述的那样启动 Laravel?
-
你使用的是 Laravel 4.1 还是 4.2?
-
@J.LaRosee 除了
create()用于静态使用这一事实之外,问题可能与$history对象无关。相反,似乎没有设置连接解析器(Model的静态属性)。如果您依赖 DatabaseServiceProvider,它可能没有正确启动。
标签: php laravel package eloquent composer-php