Skip to content

Instantly share code, notes, and snippets.

@xu-li
Last active August 29, 2015 14:04
Show Gist options
  • Save xu-li/245fe307be5a040accd3 to your computer and use it in GitHub Desktop.
Save xu-li/245fe307be5a040accd3 to your computer and use it in GitHub Desktop.
Using laravel ORM alone
<?php
// composer.json
/*
{
"require": {
"illuminate/database": "*"
}
}
*/
require_once "vendor/autoload.php";
class Book extends \Illuminate\Database\Eloquent\Model
{
}
try {
// Database information
$settings = array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'YOUR_DATABASE',
'username' => 'YOUR_DATABASE_USERNAME',
'password' => 'YOUR_DATABASE_PASSWORD',
'collation' => 'utf8_general_ci',
'charset' => 'utf8',
'prefix' => ''
);
// Ioc container
$container = new Illuminate\Container\Container();
// Make a connection
$factory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $factory->make($settings);
// Setup resolver
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
// Hook up the event dispatcher
$dispatcher = new Illuminate\Events\Dispatcher($container);
Book::setEventDispatcher($dispatcher);
Book::saved(function ($model) {
echo "Hooray!";
});
// Try it
$book = new Book;
$book->name = 'An awesome book';
$book->title = 'Using laravel ORM alone';
$book->save();
} catch (Exception $err) {
echo $err->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment