本文整理汇总了PHP中Illuminate\Database\Capsule\Manager::getDatabaseManager方法的典型用法代码示例。如果您正苦于以下问题:PHP Manager::getDatabaseManager方法的具体用法?PHP Manager::getDatabaseManager怎么用?PHP Manager::getDatabaseManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Capsule\Manager
的用法示例。
在下文中一共展示了Manager::getDatabaseManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setValidator
protected function setValidator()
{
$filesystem = new FileLoader(new Filesystem(), CONFIG_DIR . DIRECTORY_SEPARATOR . 'langs');
$translator = new Translator($filesystem, Main::$app->web->get('locale', false) ?: 'en');
$this->validator = new ValidatorFactory($translator);
$verifier = new DatabasePresenceVerifier($this->capsule->getDatabaseManager());
$this->validator->setPresenceVerifier($verifier);
}
示例2: initialize
/**
* Initializes the Eloquent ORM.
*/
public function initialize()
{
$this->capsule->bootEloquent();
if ('default' !== $this->defaultConnection) {
$this->capsule->getDatabaseManager()->setDefaultConnection($this->defaultConnection);
}
}
示例3: register
public function register()
{
$this->app->singleton('laravel.db', function ($app) {
$config = $app['config'];
// Get the default database from the configuration
$default = $config['database.default'];
// Create an capsule instance
$capsule = new CapsuleManager($app);
// Override the default value with the user's config value
$config['database.default'] = $default;
if (!isset($config['database.connections']) || empty($config['database.connections'])) {
throw new ConfigurationException("Invalid database configuration");
}
$connections = $config['database.connections'];
foreach ($connections as $name => $connectionConfig) {
$capsule->addConnection($connectionConfig, $name);
}
$capsule->setAsGlobal();
// Setup the Eloquent ORM...
if ($config['eloquent.boot'] === true) {
$capsule->bootEloquent();
}
return $capsule->getDatabaseManager();
});
}
示例4: getConnectionResolver
/**
* @param $config
* @return \Illuminate\Database\ConnectionResolverInterface
*/
protected function getConnectionResolver($config)
{
$name = $config['database.default'];
$conn = $config['database.connections'][$name];
$db = new Manager();
$db->addConnection($conn);
return $db->getDatabaseManager();
}
示例5: setUp
public function setUp()
{
$this->fileSystem = new Filesystem();
$this->cache = new Repository(new FileStore($this->fileSystem, './cache'));
$this->fileSystem->copy('./stubs.sqlite', './stubs-real.sqlite');
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => './stubs-real.sqlite', 'prefix' => '']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$this->connection = $capsule->getDatabaseManager()->connection('default');
}
示例6: initDb
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;
}
示例7: connect
/**
* 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;
}
示例8: setUpBeforeClass
/**
* Set up the tests
*/
public static function setUpBeforeClass()
{
$capsule = new Manager();
$capsule->addConnection(array('driver' => 'sqlite', 'database' => ':memory:'));
// Bind to Eloquent
$capsule->setAsGlobal();
$capsule->bootEloquent();
// Set facades
DB::setFacadeApplication(new Container());
DB::swap($capsule->getDatabaseManager());
// Reguard attributes
Model::reguard();
self::createTables($capsule);
}
示例9: register
/**
* Register database services
*
* @param PimpleContainer $di Container
*/
public function register(PimpleContainer $di)
{
$params = $this->databaseParams;
$di['dbConnection'] = function () use($params, $di) {
$capsule = $di['db'];
return $capsule->getConnection();
};
$di['dbConnectionResolver'] = function () use($params, $di) {
$connection = $di['dbConnection'];
$resolver = new ConnectionResolver(['default' => $connection]);
$resolver->setDefaultConnection('default');
return $resolver;
};
$di['dbMigrationRepository'] = function () use($params, $di) {
$resolver = $di['dbConnectionResolver'];
$repository = new DatabaseMigrationRepository($resolver, 'migrations');
return $repository;
};
$di['dbMigrator'] = function () use($params, $di) {
class_alias(Schema::class, 'Schema');
Schema::setContainer($di);
$resolver = $di['dbConnectionResolver'];
$repository = $di['dbMigrationRepository'];
$filesystem = new Filesystem();
$migrator = new Migrator($repository, $resolver, $filesystem);
return $migrator;
};
$di['db'] = function () use($params, $di) {
class_alias(DB::class, 'DB');
DB::setContainer($di);
$capsule = new Capsule();
$capsule->setEventDispatcher(new Dispatcher(new Container()));
$capsule->setAsGlobal();
$capsule->bootEloquent();
$capsule->addConnection($params);
if (class_exists(MongoDbConnection::class)) {
$capsule->getDatabaseManager()->extend('mongodb', function ($config) {
return new MongoDbConnection($config);
});
}
return $capsule;
};
$di['dbManager'] = function () use($params, $di) {
$dbCapsule = $di['db'];
return $dbCapsule->getDatabaseManager();
};
$di['db'];
}
示例10: register
/**
* 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;
});
}
示例11: __construct
/**
* Create a new Validator factory instance.
*
* @param \Illuminate\Database\Capsule\Manager $db
* @return \Illuminate\Validation\Factory
*/
public function __construct($db)
{
if (!$this->factory) {
/**
* illuminate/translation (Translator) package need for correct work of Validator
*/
$translator = new Translator('en');
$this->factory = new Factory($translator);
/**
* To set database presence verifier we need database connection instance,
* which is implements ConnectionResolverInterface. For this purpose
* Illuminate\Database\Capsule\Manager class, which we get from $db arg,
* have getDatabaseManager() function.
* With defined DatabasePresenceVerifier we can use rules such as:
* unique:table,column,except,idColumn
* exists:table,column
*/
$dbManager = $db->getDatabaseManager();
$this->factory->setPresenceVerifier(new DatabasePresenceVerifier($dbManager));
}
return $this->factory;
}
示例12: register
public function register(Container $pimple)
{
$pimple['db'] = function ($pimple) {
$db = new Manager();
$db->addConnection(['driver' => 'mysql', 'host' => $pimple['config']['db.host'], 'database' => $pimple['config']['db.database'], 'username' => $pimple['config']['db.username'], 'password' => $pimple['config']['db.password'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => $pimple['config']['db.prefix']]);
$db->setEventDispatcher($pimple['events']);
// Make this Capsule instance available globally via static methods... (optional)
$db->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$db->bootEloquent();
if ($pimple['config']['log.sql']) {
// 记录查询过的日志
$manager = $db->getDatabaseManager();
try {
$manager->listen(function ($query, $bindings = null, $time = null, $connectionName = null) use($manager, $pimple) {
// Laravel 5.2 changed the way some core events worked. We must account for
// the first argument being an "event object", where arguments are passed
// via object properties, instead of individual arguments.
if ($query instanceof \Illuminate\Database\Events\QueryExecuted) {
$bindings = $query->bindings;
$time = $query->time;
$connection = $query->connection;
$query = $query->sql;
} else {
$connection = $manager->connection($connectionName);
}
$pimple['log']->debug($query, $bindings);
$pimple['sql_log']->set($query, $bindings);
});
} catch (\Exception $e) {
$pimple['log']->debug($e->getMessage());
}
}
return $db;
};
}
示例13: __construct
/**
* Initialise a new collector
*
* @param Capsule $database
* @return void
*/
public function __construct(Capsule $database)
{
$this->connections = $database->getDatabaseManager()->getConnections();
}
示例14: function
$validator = $validation->make($data, $rules);
if ($validator->fails()) {
$errors = $validator->errors();
}
return $app->render('form.php', ['posted' => true, 'errors' => $errors, 'email' => $_POST['email']]);
});
// For a thorough example, we establish a database connection
// to drive the database presence verifier used by the validator.
// If you do not need to validate against values in the database,
// the database presence verifier and related code can be removed.
$app->get('/database', function () use($app) {
return $app->render('form.php', ['posted' => false, 'errors' => null, 'email' => '']);
});
$app->post('/database', function () use($app) {
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'mysql', 'host' => 'localhost', 'database' => 'illuminate_non_laravel', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '']);
$loader = new FileLoader(new Filesystem(), 'lang');
$translator = new Translator($loader, 'en');
$presence = new DatabasePresenceVerifier($capsule->getDatabaseManager());
$validation = new Factory($translator, new Container());
$validation->setPresenceVerifier($presence);
$data = ['email' => $_POST['email']];
$rules = ['email' => 'required|email|unique:users'];
$errors = null;
$validator = $validation->make($data, $rules);
if ($validator->fails()) {
$errors = $validator->errors();
}
return $app->render('form.php', ['posted' => true, 'errors' => $errors, 'email' => $_POST['email']]);
});
$app->run();
示例15: initDb
private function initDb()
{
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'host' => 'localhost', 'database' => ':memory:', 'prefix' => '']);
$capsule->setEventDispatcher(new Dispatcher(new Container()));
$capsule->setAsGlobal();
$capsule->bootEloquent();
Capsule::schema()->create('settings', function ($table) {
$table->increments('id');
$table->string('key')->index()->unique();
$table->string('value')->longText();
});
return $capsule->getDatabaseManager();
}