Created
March 4, 2015 19:24
-
-
Save etrepat/90057c1f9f4e69b78191 to your computer and use it in GitHub Desktop.
Using Baum with stand-alone Eloquent (Capsule) - Laravel 5.0.x
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 | |
require __DIR__ . '/vendor/autoload.php'; | |
use Illuminate\Database\Capsule\Manager as DB; | |
use Baum\Node; | |
// Initialize Capsule | |
$capsule = new DB; | |
// Add connection settings | |
$capsule->addConnection(require(__DIR__ . '/database.php')); | |
// Set the event dispatcher so the model events work | |
$capsule->setEventDispatcher(new Illuminate\Events\Dispatcher(new Illuminate\Container\Container)); | |
// Initialize Eloquent | |
$capsule->bootEloquent(); | |
// This is so we can reference Illuminate\Database\Capsule\Manager statically | |
$capsule->setAsGlobal(); | |
// Sample Category class | |
class Category extends Node { | |
protected $table = 'categories'; | |
public $timestamps = false; | |
} | |
// Minimal migrator (table creation) and a sample seeder class so | |
// we can do some test queries | |
class CategoryMigrator { | |
public function up() { | |
DB::schema()->dropIfExists('categories'); | |
DB::schema()->create('categories', function($t) { | |
$t->increments('id'); | |
$t->integer('parent_id')->nullable(); | |
$t->integer('lft')->nullable(); | |
$t->integer('rgt')->nullable(); | |
$t->integer('depth')->nullable(); | |
$t->string('name'); | |
}); | |
} | |
public function down() { | |
DB::schema()->drop('categories'); | |
} | |
} | |
class CategorySeeder { | |
public function run() { | |
DB::table('categories')->delete(); | |
Category::unguard(); | |
Category::create(array('id' => 1, 'name' => 'Root 1' , 'lft' => 1 , 'rgt' => 10 , 'depth' => 0)); | |
Category::create(array('id' => 2, 'name' => 'Child 1' , 'lft' => 2 , 'rgt' => 3 , 'depth' => 1, 'parent_id' => 1)); | |
Category::create(array('id' => 3, 'name' => 'Child 2' , 'lft' => 4 , 'rgt' => 7 , 'depth' => 1, 'parent_id' => 1)); | |
Category::create(array('id' => 4, 'name' => 'Child 2.1', 'lft' => 5 , 'rgt' => 6 , 'depth' => 2, 'parent_id' => 3)); | |
Category::create(array('id' => 5, 'name' => 'Child 3' , 'lft' => 8 , 'rgt' => 9 , 'depth' => 1, 'parent_id' => 1)); | |
Category::create(array('id' => 6, 'name' => 'Root 2' , 'lft' => 11 , 'rgt' => 12 , 'depth' => 0)); | |
Category::reguard(); | |
if ( DB::connection()->getDriverName() === 'pgsql' ) { | |
$tablePrefix = DB::connection()->getTablePrefix(); | |
$sequenceName = $tablePrefix . 'categories_id_seq'; | |
DB::connection()->statement('ALTER SEQUENCE ' . $sequenceName . ' RESTART WITH 7'); | |
} | |
} | |
} | |
// Sample code: | |
// Migrate the DB, seed it and run a simple query. | |
$migrator = new CategoryMigrator; | |
$migrator->up(); | |
$seeder = new CategorySeeder; | |
$seeder->run(); | |
$root = Category::where('name', '=', 'Root 1')->first(); | |
$descendants = $root->descendants()->get(); | |
var_dump($descendants); |
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
{ | |
"require": { | |
"php": ">=5.4.0", | |
"illuminate/database": "5.*", | |
"illuminate/events": "5.*", | |
"baum/baum": "~1.1.0" | |
} | |
} |
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 | |
// In-memory SQLite DB | |
return array( | |
'driver' => 'sqlite', | |
'database' => ':memory:', | |
'prefix' => '' | |
); | |
// // Postgres | |
// return array( | |
// 'driver' => 'pgsql', | |
// 'host' => 'localhost', | |
// 'database' => 'baum_test', | |
// 'username' => 'postgres', | |
// 'password' => 'postgres', | |
// 'charset' => 'utf8', | |
// 'prefix' => '', | |
// 'schema' => 'public', | |
// ); | |
// // MySQL | |
// return array( | |
// 'driver' => 'mysql', | |
// 'host' => 'localhost', | |
// 'database' => 'baum_test', | |
// 'username' => 'mysql', | |
// 'password' => 'mysql', | |
// 'charset' => 'utf8', | |
// 'collation' => 'utf8_unicode_ci', | |
// 'prefix' => '', | |
// ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment