Skip to content

Instantly share code, notes, and snippets.

@etrepat
Last active November 21, 2016 15:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etrepat/aa0ceb144d1175cdee5e to your computer and use it in GitHub Desktop.
Save etrepat/aa0ceb144d1175cdee5e to your computer and use it in GitHub Desktop.
Using Baum with stand-alone Eloquent (Capsule) - Laravel 4.2.x
<?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);
// 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);
{
"require": {
"php": ">=5.4.0",
"illuminate/database": "4.2.*",
"baum/baum": "~1.0.0"
}
}
<?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