示例#1
1
 private function setDatabase($dbConfig)
 {
     $capsule = new Capsule();
     $capsule->addConnection($dbConfig, $this->connName);
     $this->set('db', $capsule->getConnection($this->connName));
     //set model conn for all models
     $resolver = new \Illuminate\Database\ConnectionResolver();
     $resolver->addConnection($this->connName, $capsule->getConnection($this->connName));
     $resolver->setDefaultConnection($this->connName);
     \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
 }
示例#2
1
 /**
  * Get a registered connection instance.
  *
  * @param string $name
  * @return \Illuminate\Database\Connection
  */
 public function getConnection($connection)
 {
     return $this->capsule->getConnection($connection);
 }
 /**
  * Returns an instance of the Illuminate Database connection having the specified name, or the default connection if
  * no name is given.
  *
  * @param string $connectionName [optional] A connection name, if you want to use a connection other than the default.
  * @return \Illuminate\Database\Connection
  */
 public function connection($connectionName = 'default')
 {
     $connectionName = $connectionName ?: $this->defaultConnection;
     try {
         return $this->manager->getConnection($connectionName);
     } catch (\InvalidArgumentException $e) {
         $connectionName = $connectionName ?: 'default';
         $con = $this->connections->get($connectionName);
         $this->manager->addConnection($con->getProperties(), $connectionName);
         return $this->manager->getConnection($connectionName);
     }
 }
 /**
  * @param array $config
  * @return \Slim\Slim
  */
 public function dispatch($config)
 {
     \Slim\Environment::mock(array('REQUEST_METHOD' => 'HEAD', 'PATH_INFO' => '/'));
     $config['debug'] = false;
     // ignore pretty exceptions
     $slim = new \Slim\Slim($config);
     $manager = new \Slim\Middleware\SessionManager($slim);
     $manager->setDbConnection($this->capsule->getConnection());
     $session = new \Slim\Middleware\Session($manager);
     $slim->add($session);
     $slim->run();
     return $slim;
 }
示例#5
0
 /**
  * Initialize illuminate/database
  *
  * @param Yaf\Dispatcher $dispatcher
  */
 protected function _initORM(Yaf\Dispatcher $dispatcher)
 {
     $dbConfig = Yaf\Application::app()->getConfig()->database->toArray();
     $db = new Capsule();
     $db->addConnection($dbConfig);
     $db->bootEloquent();
     Yaf\Registry::set('db', $db->getConnection());
 }
示例#6
0
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     $config = (require __DIR__ . '/../config.php');
     $capsule = new Manager();
     $capsule->addConnection($config);
     $capsule->bootEloquent();
     Schema::setConnection($capsule->getConnection('default'));
     DB::setConnection($capsule->getConnection('default'));
     // run the migrations
     $migration_repo = new DatabaseMigrationRepository(Model::getConnectionResolver(), 'migrations');
     if (!$migration_repo->repositoryExists()) {
         $migration_repo->createRepository();
     }
     $migrator = new Migrator($migration_repo, Model::getConnectionResolver(), new Filesystem());
     $migrator->rollback();
     $migrator->run(__DIR__ . '/../../src/migrations');
     static::loadFixtures();
 }
示例#7
0
 protected function initDb()
 {
     $capsule = new Manager();
     $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'fetch' => PDO::FETCH_CLASS]);
     $capsule->getConnection()->setFetchMode(PDO::FETCH_CLASS);
     $capsule->setAsGlobal();
     $capsule->bootEloquent();
     $this->db = $capsule->getDatabaseManager();
     $this->createTables();
     $this->initMuffing();
     return $this->db;
 }
示例#8
0
 /**
  * Setup the database service.
  *
  * @param \Slim\Slim $app The application instance.
  */
 public static function setup(Slim $app)
 {
     $manager = new Manager();
     $manager->addConnection(['driver' => $app->config('db.driver'), 'host' => $app->config('db.host'), 'database' => $app->config('db.database'), 'username' => $app->config('db.username'), 'password' => $app->config('db.password'), 'charset' => $app->config('db.charset'), 'collation' => $app->config('db.collation'), 'prefix' => $app->config('db.prefix')]);
     $manager->setEventDispatcher(new Dispatcher(new Container()));
     $manager->setAsGlobal();
     $manager->bootEloquent();
     // Setting database connection to use the same time zone as
     // application.
     $manager->getConnection()->statement('SET time_zone = ?', [Carbon::now()->offsetHours . ':00']);
     $app->database = $manager;
 }
示例#9
0
 protected function getCapsule()
 {
     $capsule = new Capsule();
     $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
     /**
      * Swap PDO instance so that we're using the same in-memory
      * database migrated by Phinx.
      */
     $capsule->getConnection()->setPdo($this->phinxPdo);
     $capsule->setAsGlobal();
     return $capsule;
 }
示例#10
0
 /**
  * Start up eloquent
  *
  * @param array|null $credentials
  *
  * @return \Illuminate\Database\Connection|null
  */
 public function connect(array $credentials = null)
 {
     if ($credentials = $credentials ?? $this->getDsn()) {
         $this->capsule->getDatabaseManager()->purge($this->capsule->getDatabaseManager()->getDefaultConnection());
         $this->capsule->addConnection(array_merge(['driver' => 'mysql', 'host' => 'localhost', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => ''], $credentials));
         $this->capsule->setEventDispatcher($this->dispatcher);
         $this->capsule->setAsGlobal();
         $this->capsule->bootEloquent();
         $this->connection = $this->capsule->getConnection();
         $this->connection->enableQueryLog();
         return $this->connection;
     }
     return null;
 }
 /**
  * Register the Illuminate Database service
  *
  * @param Silex\Application $app
  */
 public function register(Application $app)
 {
     $app['database'] = $app->share(function () use($app) {
         $capsule = new Capsule();
         $capsule->addConnection($app['database.connection']);
         $capsule->setEventDispatcher(new Dispatcher(new Container()));
         if (isset($app['database.connection']['logging']) && $app['database.connection']['logging'] === true) {
             $capsule->getConnection()->enableQueryLog();
         }
         $capsule->setAsGlobal();
         $capsule->bootEloquent();
         return $capsule;
     });
 }
 public function connect_db()
 {
     $localPDO = \DB::getPDO();
     $capsule = new Capsule();
     $capsule->addConnection($this->_DB, 'remote');
     $connection = $capsule->getConnection('remote');
     $remotePDO = $connection->getPdo();
     $class = '\\CODOF\\Importer\\Drivers\\' . $this->importer;
     $this->fetch = new $class($remotePDO);
     $this->connected = true;
     //\CODOF\DB::$connected ? true : false;
     $this->fetch->max_rows = $this->max_rows;
     $this->fetch->set_prefix($this->_DB['prefix']);
     $this->im = new Import($localPDO, $this->fetch);
 }
示例#13
0
 protected function databaseHandler()
 {
     $Database = new Manager();
     $config = $this->config['connection'];
     $Database->addConnection($config);
     $Connection = $Database->getConnection('default');
     $Schema = $Connection->getSchemaBuilder();
     if (!$Schema->hasTable($this->config['table'])) {
         $Schema->create($this->config['table'], function ($Table) {
             $Table->string('id')->unique();
             $Table->text('payload');
             $Table->integer('last_activity');
         });
     }
     return new DatabaseSessionHandler($Connection, $this->config['table']);
 }
 /**
  * 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;
     });
 }
示例#15
0
 public function register(Application $app)
 {
     // boot Eloquent
     $defaultConn = $app->getConfig('db.default');
     $capsule = new Capsule();
     $capsule->addConnection($app->getConfig("db.connections.{$defaultConn}"));
     $capsule->setAsGlobal();
     $capsule->bootEloquent();
     $capsule->connection();
     // register Database in container
     $app->set("Database", function ($name = null) use($app) {
         $name = $name ?: $app->getConfig('db.default');
         $config = $app->getConfig("db.connections.{$name}");
         $capsule = new Capsule();
         $capsule->addConnection($config, $name);
         return $capsule->getConnection($name);
     });
 }
示例#16
0
 /**
  * Create database connection
  *
  * @return Capsule
  */
 public static function create(ServiceContainer $app)
 {
     $capsule = new Capsule();
     $config = $app->config['db'];
     $capsule->addConnection($config);
     // Set the event dispatcher used by Eloquent models
     $capsule->setEventDispatcher(new CapsuleDispatcher());
     // Make this Capsule instance available globally via static methods
     $capsule->setAsGlobal();
     $connection = $capsule->getConnection();
     $connection->disableQueryLog();
     // Set default to array (instead of stdObject)
     $connection->setFetchMode(PDO::FETCH_ASSOC);
     // Convert column names to lower case.
     $pdo = $connection->getPdo();
     $pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
     // Enable UTF-8 encoding
     $pdo->exec('SET NAMES utf8;');
     return $capsule;
 }
 /**
  * Register the Capsule Service.
  *
  * @param PimpleContainer $app
  **/
 public function register(PimpleContainer $app)
 {
     $app['capsule.connectionDefault'] = ['driver' => 'mysql', 'host' => 'localhost', 'port' => 3306, 'database' => null, 'username' => null, 'password' => null, 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null];
     $app['capsule.connections'] = [];
     $app['capsule.container'] = function (Application $app) {
         return new CapsuleContainer();
     };
     $app['capsule.dispatcher'] = function (Application $app) {
         return new Dispatcher($app['capsule.container']);
     };
     $app['capsule.options'] = ['setAsGlobal' => true, 'bootEloquent' => true, 'enableQueryLog' => true];
     $app['capsule'] = function (Application $app) {
         $capsule = new Capsule($app['capsule.container']);
         // Set the event dispatcher used by Eloquent models...
         if (!empty($app['capsule.dispatcher'])) {
             $capsule->setEventDispatcher($app['capsule.dispatcher']);
         }
         // Connections
         foreach ($app['capsule.connections'] as $connectionName => $connectionConfig) {
             $connectionConfig += $app['capsule.connectionDefault'];
             $capsule->addConnection($connectionConfig, $connectionName);
             if ($app['capsule.options']['enableQueryLog']) {
                 $capsule->getConnection($connectionName)->enableQueryLog();
             }
         }
         // Make this Capsule instance available globally via static methods...
         if (!empty($app['capsule.options']['setAsGlobal'])) {
             $capsule->setAsGlobal();
         }
         // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
         if (!empty($app['capsule.options']['bootEloquent'])) {
             $capsule->bootEloquent();
         }
         return $capsule;
     };
 }
示例#18
0
 /**
  * Get a connection instance from the global manager.
  *
  * @param  string  $connection
  * @return \Illuminate\Database\Connection
  */
 public static function connection($connection = null)
 {
     return static::$instance->getConnection($connection);
 }
示例#19
0
        $xml = simplexml_load_file(TFS_ROOT . '/data/XML/vocations.xml');
        if (property_exists($xml, 'vocation')) {
            return $xml;
        }
    }
});
////////////////////////// CONNECT TO DATABASE /////////////////////////////////
// Bootstrap Eloquent ORM
// https://github.com/illuminate/database
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'mysql', 'host' => $DevAAC->tfsConfigFile['mysqlHost'], 'database' => $DevAAC->tfsConfigFile['mysqlDatabase'], 'username' => $DevAAC->tfsConfigFile['mysqlUser'], 'password' => $DevAAC->tfsConfigFile['mysqlPass'], 'charset' => 'latin1', 'collation' => 'latin1_swedish_ci', 'prefix' => '']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
try {
    if (strpos($capsule->getConnection()->getPdo()->getAttribute(PDO::ATTR_CLIENT_VERSION), 'mysqlnd') === false) {
        die('PHP PDO is using non-native MySQL extension, php-mysqlnd native extension is required. You most likely have to execute: apt-get install php5-mysqlnd');
    }
} catch (PDOException $e) {
    http_response_code(503);
    die(json_encode(array('code' => 503, 'message' => 'Server is temporarily unavailable due to a MySQL connection problem.'), JSON_PRETTY_PRINT));
}
////////////////////// SERVE API DOCS WITH Swagger ///////////////////////////////
// http://zircote.com/swagger-php/using_swagger.html
// https://github.com/zircote/swagger-php/blob/master/library/Swagger/Swagger.php
use Swagger\Swagger;
$DevAAC->get(ROUTES_API_PREFIX . '/docs(/:path)', function ($path = '/') use($DevAAC) {
    $swagger = new Swagger('../', '../vendor');
    $DevAAC->response->headers->set('Access-Control-Allow-Origin', '*');
    $DevAAC->response->headers->set('Content-Type', 'application/json');
    if ($path != '/') {
 /**
  * Constructor
  *
  * @param \Illuminate\Database\Capsule\Manager $capsule
  */
 public function __construct(\Illuminate\Database\Capsule\Manager $capsule)
 {
     $this->capsule = $capsule;
     $this->connection = new Capsule5Connection($this, $capsule);
     $this->resource = $capsule->getConnection()->getPdo();
 }
示例#21
0
 * 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();
/**
 * Binds the Capsule to the Slim Container,
 * allowing the user to resolve it later.
 */
$app->container->singleton('db', function () use($capsule) {
    return $capsule->getConnection();
});
示例#22
0
 /**
  * Get the query log
  *
  * @return array
  */
 public function getQueryLog()
 {
     return $this->capsule->getConnection()->getQueryLog();
 }
 public function getDatabaseProvider()
 {
     $capsule = new Manager();
     $capsule->addConnection(['driver' => 'sqlite', 'database' => self::SQLITE_PATH]);
     return new IlluminateDatabaseProvider($capsule->getConnection('default'));
 }
示例#24
0
 /**
  * @param \Illuminate\Database\Capsule\Manager $manager
  * @param \Wandu\Database\Migrator\Configuration $config
  */
 public function __construct(Manager $manager, Configuration $config)
 {
     $this->tableName = $config->getTable();
     $this->connection = $manager->getConnection($config->getConnection());
 }
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\Plugin\ListWith;
try {
    $adapter = new Local($config['uploads_dir'] . '/sms/');
    $filesystem = new Filesystem($adapter);
    $filesystem->addPlugin(new ListWith());
    $capsule = new Capsule();
    foreach ($config['databases'] as $name => $database) {
        $capsule->addConnection($database, $name);
    }
    $capsule->setAsGlobal();
    $capsule->bootEloquent();
    if ($filesystem->has('last.sms')) {
        $lastId = $filesystem->read('last.sms');
        $res = $capsule->getConnection($connection_name)->select('select ID as sms_id, SenderNumber as author, TextDecoded as plainText from inbox where ID > ?', array($lastId));
    } else {
        $lastId = 0;
        $res = $capsule->getConnection($connection_name)->select('select ID as sms_id, SenderNumber as author, TextDecoded as plainText from inbox');
    }
    foreach ($res as $sms) {
        //    [sms_id] => 2
        //    [author] => +393395039915
        //    [plainText] => 0054006500640074
        $sms_id = $sms->sms_id;
        $lastId = $sms_id;
        $author = $sms->author;
        $plainText = $sms->plainText;
        preg_match_all('/#(\\w+)/', $plainText, $matches);
        $hashtags = array();
        if (isset($matches) && count($matches) > 0) {
 public function __construct($hostname = 'localhost', $username = '******', $password = '', $database = 'nodatabase')
 {
     $capsule = new Capsule();
     $capsule->addConnection(array('driver' => 'mysql', 'host' => $hostname, 'database' => $database, 'username' => $username, 'password' => $password, 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => ''));
     $this->db = $capsule->getConnection();
 }
示例#27
-1
 public function setupDatabase($db = array())
 {
     //add default routes
     if (empty($db)) {
         $db = (include __DIR__ . '/../App/Config/db.php');
     }
     $capsule = new Capsule();
     $capsule->addConnection($db, 'default');
     $capsule->container['config']['database.fetch'] = \PDO::FETCH_CLASS;
     $capsule->bootEloquent();
     // setup db
     $this->db = $capsule->getConnection('default');
 }
示例#28
-1
文件: DB.php 项目: newcart/system
 /**
  * Pega a conexao do banco
  * @param string $name
  * @return \Illuminate\Database\Connection
  */
 public function getConnection($name = 'default')
 {
     return $this->capsule->getConnection($name);
 }
示例#29
-1
 protected function assertQueryCount($expectedCount)
 {
     $log = $this->capsule->getConnection()->getQueryLog();
     $this->assertEquals($expectedCount, count($log), 'Expected ' . $expectedCount . ' queries, got ' . count($log));
 }
示例#30
-1
// 'a', 'b', 'c', 'd', 'e', and 'f'.
$randomString = $generator->generateString(6);
print_r($randomString);
echo PHP_EOL;
$database = ['driver' => 'mysql', 'host' => '10.59.72.31', 'database' => 'gold_db', 'username' => 'fuqiang147', 'password' => 'fuqiang147', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => ''];
use Illuminate\Database\Capsule\Manager as Capsule;
//如果你不喜欢这个名称,as DB;就好
$capsule = new Capsule();
// 创建链接
$capsule->addConnection($database, 'seed');
// 设置全局静态可访问
$capsule->setAsGlobal();
$table = 't_crm_soa_order';
// 启动Eloquent
$capsule->bootEloquent();
$conn = $capsule->getConnection('seed');
$grammar = $conn->getQueryGrammar();
$processor = $conn->getPostProcessor();
$sql = "select column_name from information_schema.columns where table_schema = 'gold_db' and table_name =\r\n't_crm_soa_order'";
$sql = 'desc t_crm_soa_order';
//dd($conn->getColumnListing($table));
//$conn->statement();
$ret = $conn->select($sql);
$queryBuilder = new Illuminate\Database\Query\Builder($conn, $grammar, $processor);
//dd($queryBuilder->getColumnListing($table));
dd($ret);
exit;
$conn->affectingStatement();
$conn->commit();
//dd($conn->getColumnListing($table));
//dd($conn->statement('desc t_crm_soa_order'));