Esempio n. 1
0
 protected function createConnection()
 {
     $this->capsule = new Capsule();
     $this->capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/db/testing.sqlite', 'prefix' => '']);
     $this->capsule->setFetchMode(PDO::FETCH_CLASS);
     $this->capsule->setAsGlobal();
     $this->capsule->bootEloquent();
     $this->capsule->getConnection()->enableQueryLog();
 }
 /**
  * Register Illuminate with the application
  *
  * @param array $config
  * @param Application $app
  * @return void
  */
 public function register(Application $app)
 {
     // Set some sane defaults
     $defaults = array('boot' => true, 'eloquent' => true, 'fetch' => PDO::FETCH_CLASS, 'global' => true, 'default' => 'default', 'connections' => array('default' => array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'silex', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => null)));
     // Merge in any passed configuration
     $this->config = array_merge($defaults, $app['db.config']);
     // Make sure all connections have all required fields
     if (isset($this->config['connections'])) {
         foreach ($this->config['connections'] as $index => $connection) {
             $this->config['connections'][$index] = array_merge($defaults['connections']['default'], $connection);
         }
     }
     // Create a container for the database pool
     $app['db.container'] = $app->share(function () {
         return new Container();
     });
     // Create the connections to the datbase
     $app['db'] = $app->share(function () use($app) {
         $db = new Capsule($app['db.container']);
         // Set PDO fetch mode as per configuration
         $db->setFetchMode($this->config['fetch']);
         // Set as global, use eloquent as per configuration
         if ($this->config['eloquent']) {
             $db->bootEloquent();
         }
         if ($this->config['global']) {
             $db->setAsGlobal();
         }
         // Set up the event dispatcher if we have it available
         if (class_exists('Illuminate\\Events\\Dispatcher')) {
             $db->setEventDispatcher(new Dispatcher($app['db.container']));
         }
         // Initialise each connection
         foreach ($this->config['connections'] as $connection => $options) {
             $db->addConnection(array_replace($this->config['connections'][$this->config['default']], $options), $connection);
             // If we're in debug mode we're going to want to use the query log, otherwise leave it disabled
             // to speed up queries and reduce memory usage
             if ($app['debug']) {
                 $db->getConnection($connection)->enableQueryLog();
             }
         }
         // Finally set default connection
         $db->getDatabaseManager()->setDefaultConnection($this->config['default']);
         return $db;
     });
 }
Esempio n. 3
0
<?php

use Illuminate\Database\Capsule\Manager as Capsule;
require __DIR__ . '/../comments/start.php';
// Connection to the old database.
$capsule = new Capsule();
$capsule->addConnection(require __DIR__ . '/database.php');
$capsule->setFetchMode(PDO::FETCH_OBJ);
$db = $capsule->getDatabaseManager();
// Get the comment root id for the given parent id.
function get_root_id($id)
{
    global $db;
    $comment = $db->table('comments')->where('id', $id)->first(['id', 'parent']);
    if (!$comment) {
        return $id;
    }
    if (!empty($comment->parent)) {
        return get_root_id($comment->parent);
    }
    return $comment->id;
}
// Select all comments from old table.
$comments = $db->table('comments')->get();
$status = ['pending', 'approved', 'spam'];
Comment::unguard();
// Insert the old comments into the new table.
foreach ($comments as $comment) {
    if (empty($comment->parent)) {
        $rootId = null;
    } else {
Esempio n. 4
0
 * Database package.
 *
 * @var Illuminate\Database\Capsule\Manager
 */
$capsule = new Capsule();
/**
 * Gets the configuration from the database
 * configuration file.
 *
 * @var array
 */
$config = getconfig('database');
/**
 * Sets the PDO Fetch Mode.
 */
$capsule->setFetchMode($config['fetch']);
/**
 * Adds the configuration of the default
 * driver setting.
 */
$capsule->addConnection($config['connections'][$config['default']]);
/**
 * Set the Capsule as a global to allow
 * the usage of static methods.
 */
$capsule->setAsGlobal();
/**
 * Booting Eloquent. Enjoy.
 */
$capsule->bootEloquent();
/**