Tuesday, 15 February 2011

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



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

i'm building bundle in laravel 4 getting non-object error when attempting access db seems instantiated object. here's setup:

the config , 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; utilize illuminate\database\eloquent\model 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: phone call fellow member function connection() on non-object in /home/vagrant/project/vendor/laravel/framework/src/illuminate/database/eloquent/model.php on line 2894

i believe i'm missing fundamental , under nose. , help!

edit:

here class that's instantiating chathistory , calling write:

namespace vendor\chat; utilize ratchet\messagecomponentinterface; utilize ratchet\connectioninterface; utilize vendor\chat\client; utilize vendor\chat\message; utilize vendor\chat\chathistory; utilize 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 $client) { if($client->getsocket() === $conn) { homecoming $client; } } homecoming null; } }

the fact db isn't available suggest eloquent isn't beingness loaded top?

answer:

bootstrap bundle in service provider's boot method.

explanation:

since you're developing bundle used laravel, there's no point in making own capsule instance. can utilize eloquent directly.

your problem seems stem db/eloquent not beingness set yet time code hits it.

you have not shown service provider, i'm guessing you're using 1 , doing in register method.

since bundle depends on different service provider (databaseserviceprovider) wired prior own execution, right place bootstrap bundle in service provider's boot method.

here's quote the docs:

the register method called when service provider registered, while boot command called right before request routed.

so, if actions in service provider rely on service provider beingness registered [...] should utilize boot method.

php laravel package eloquent composer-php

No comments:

Post a Comment