-
-
Save BigBlueHat/5877827 to your computer and use it in GitHub Desktop.
revised to get this to work with Eloquent 4.0.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Post | |
{ | |
protected $table = 'posts'; | |
/** | |
* You can define your own custom boot method. | |
* | |
* @return void | |
**/ | |
public static function boot() | |
{ | |
parent::boot(); | |
static::creating(function($post) { | |
echo 'Creating (from boot)'; | |
}); | |
} | |
/** | |
* You can access the database connection in a static model method with the resolver. | |
* | |
* @return void | |
**/ | |
static public function doSomething() | |
{ | |
$db = static::resolveConnection(); | |
} | |
} | |
class PostObserver | |
{ | |
public function creating($post) | |
{ | |
echo 'Creating (from observer)'; | |
} | |
public function created($post) | |
{ | |
echo 'Created'; | |
} | |
public function updating($post) | |
{ | |
echo 'Updating'; | |
} | |
public function updated($post) | |
{ | |
echo 'Updated'; | |
} | |
public function deleting($post) | |
{ | |
echo 'Deleting'; | |
} | |
public function deleted($post) | |
{ | |
echo 'Deleted'; | |
} | |
public function saving($post) | |
{ | |
echo 'Saving'; | |
} | |
public function saved($post) | |
{ | |
echo 'Saved'; | |
} | |
} | |
// Setup Capsule. | |
// See: https://github.com/illuminate/database | |
use Illuminate\Database\Capsule\Manager as Capsule; | |
$capsule = new Capsule; | |
$capsule->addConnection(array( | |
'driver' => 'mysql', | |
'host' => 'localhost', | |
'database' => 'database', | |
'username' => 'root', | |
'password' => 'password', | |
'charset' => 'utf8', | |
'collation' => 'utf8_unicode_ci', | |
'prefix' => '', | |
)); | |
// Set the event dispatcher used by Eloquent models... (optional). | |
use Illuminate\Events\Dispatcher; | |
### had to pass in a Container object as Dispatcher isn't making what it needs | |
### https://github.com/illuminate/events/blob/master/Dispatcher.php#L280 fails | |
### thanks to https://github.com/laravel/framework/blob/master/src/Illuminate/Events/Dispatcher.php#L41 | |
### so I'm passing it in by hand... | |
use Illuminate\Container\Container; | |
$capsule->setEventDispatcher(new Dispatcher(new Container)); | |
// Set the cache manager instance used by connections... (optionaL). | |
use Illuminate\Support\Container; | |
use Illuminate\Cache\CacheManager; | |
$cache = new CacheManager(new Container); | |
$cache->driver('apc'); | |
$capsule->setCacheManager($cache); | |
// Make this Capsule instance available globally via static methods... (optional). | |
$capsule->setAsGlobal(); | |
// Setup the Eloquent ORM... | |
### NOT optional--at least for me | |
### if this is called before the setEventDispatcher() then events don't fire. | |
$capsule->bootEloquent(); | |
// Observe / Forget Post events. | |
// See: https://github.com/laravel/framework/issues/1339 | |
$observer = new PostObserver; | |
$capsule->getContainer()->instance('PostObserver', $observer); | |
Post::observe($observer); | |
Post::forget('saved'); | |
// Create / Update / Delete a Post model. | |
$post = new Post; | |
$post->title = 'Hello, World!'; | |
$post->save(); | |
$post->delete(); | |
// Fill a Post model. | |
$post = new Post; | |
$post->fill(array('title' => 'Hello, World!')); | |
// Get the query log. | |
$queries = $capsule->connection()->getQueryLog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment