本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::setConnectionResolver方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::setConnectionResolver方法的具体用法?PHP Model::setConnectionResolver怎么用?PHP Model::setConnectionResolver使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::setConnectionResolver方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initialize
/**
* Initialize the Laravel framework.
* @param SymfonyRequest $request
*/
private function initialize($request = null)
{
// Store a reference to the database object
// so the database connection can be reused during tests
$oldDb = null;
if ($this->app['db'] && $this->app['db']->connection()) {
$oldDb = $this->app['db'];
}
// The module can login a user with the $I->amLoggedAs() method,
// but this is not persisted between requests. Store a reference
// to the logged in user to simulate this.
$loggedInUser = null;
if ($this->app['auth'] && $this->app['auth']->check()) {
$loggedInUser = $this->app['auth']->user();
}
// Load the application object
$this->app = $this->kernel = $this->loadApplication();
// Set the request instance for the application
if (is_null($request)) {
$appConfig = (require $this->module->config['project_dir'] . 'config/app.php');
$request = SymfonyRequest::create($appConfig['url']);
}
$this->app->instance('request', Request::createFromBase($request));
$this->app->instance('middleware.disable', $this->module->config['disable_middleware']);
// Bootstrap the application
$this->app->make('Illuminate\\Contracts\\Http\\Kernel')->bootstrap();
// Restore the old database object if available
if ($oldDb) {
$this->app['db'] = $oldDb;
Model::setConnectionResolver($this->app['db']);
}
// If there was a user logged in restore this user.
// Also reload the user object from the user provider to prevent stale user data.
if ($loggedInUser) {
$refreshed = $this->app['auth']->getProvider()->retrieveById($loggedInUser->getAuthIdentifier());
$this->app['auth']->setUser($refreshed ?: $loggedInUser);
}
$this->module->setApplication($this->app);
}
示例2: initialize
/**
* Initialize the Laravel framework.
*/
private function initialize()
{
// Store a reference to the database object
// so the database connection can be reused during tests
$oldDb = null;
if ($this->app['db'] && $this->app['db']->connection()) {
$oldDb = $this->app['db'];
}
// The module can login a user with the $I->amLoggedAs() method,
// but this is not persisted between requests. Store a reference
// to the logged in user to simulate this.
$loggedInUser = null;
if ($this->app['auth'] && $this->app['auth']->check()) {
$loggedInUser = $this->app['auth']->user();
}
$this->app = $this->kernel = $this->loadApplication();
$this->app->make('Illuminate\\Contracts\\Http\\Kernel')->bootstrap();
// Set the base url for the Request object
$url = $this->app['config']->get('app.url', 'http://localhost');
$this->app->instance('request', Request::createFromBase(SymfonyRequest::create($url)));
if ($oldDb) {
$this->app['db'] = $oldDb;
Model::setConnectionResolver($this->app['db']);
}
// If there was a user logged in restore this user.
// Also reload the user object from the user provider to prevent stale user data.
if ($loggedInUser) {
$refreshed = $this->app['auth']->getProvider()->retrieveById($loggedInUser->getAuthIdentifier());
$this->app['auth']->setUser($refreshed ?: $loggedInUser);
}
$this->module->setApplication($this->app);
}
示例3: setDatabase
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);
}
示例4: __construct
protected function __construct()
{
$this->app = new Container();
// Register mock instances with IoC container
$config = $this->getConfig();
$this->app->singleton('config', function () use($config) {
return $config;
});
list($connector, $manager) = $this->getDatabase();
$this->app->singleton('db.factory', function () use($connector) {
return $connector;
});
$this->app->singleton('db', function () use($manager) {
return $manager;
});
$auth = $this->getAuth();
$this->app->singleton('auth', function () use($auth) {
return $auth;
});
$dispatcher = new Dispatcher($this->app);
$this->app->singleton('events', function () use($dispatcher) {
return $dispatcher;
});
Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']);
}
示例5: initialize
/**
* Initialize the Laravel Framework.
*
* @throws ModuleConfig
*/
private function initialize()
{
// Store a reference to the database object
// so the database connection can be reused during tests
$oldDb = null;
if ($this->app['db'] && $this->app['db']->connection()) {
$oldDb = $this->app['db'];
}
// Store the current value for the router filters
// so it can be reset after reloading the application
$oldFiltersEnabled = null;
if ($router = $this->app['router']) {
$property = new \ReflectionProperty(get_class($router), 'filtering');
$property->setAccessible(true);
$oldFiltersEnabled = $property->getValue($router);
}
$this->app = $this->loadApplication();
$this->kernel = $this->getStackedClient();
$this->app->boot();
// Reset the booted flag of the Application object
// so the app will be booted again if it receives a new Request
$property = new \ReflectionProperty(get_class($this->app), 'booted');
$property->setAccessible(true);
$property->setValue($this->app, false);
if ($oldDb) {
$this->app['db'] = $oldDb;
Model::setConnectionResolver($this->app['db']);
}
if (!is_null($oldFiltersEnabled)) {
$oldFiltersEnabled ? $this->app['router']->enableFilters() : $this->app['router']->disableFilters();
}
$this->module->setApplication($this->app);
}
示例6: createSentry
/**
* Creates a new instance of Sentry.
*
* @return \Cartalyst\Sentry\Sentry
* @throws \RuntimeException
*/
public static function createSentry()
{
// Get some resources
$ci =& get_instance();
$ci->load->driver('session');
// If Eloquent doesn't exist, then we must assume they are using their own providers.
if (class_exists('Illuminate\\Database\\Eloquent\\Model')) {
$ci->load->database();
// Let's connect and get the PDO instance
$method = $ci->db->pconnect ? 'db_pconnect' : 'db_connect';
$pdo = $ci->db->{$method}();
// Validate PDO
if (!$pdo instanceof PDO) {
throw new \RuntimeException("Sentry will only work with PDO database connections.");
}
// Setup PDO
foreach (static::$pdoOptions as $key => $value) {
$pdo->setAttribute($key, $value);
}
$driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
$tablePrefix = substr($ci->db->dbprefix('.'), 0, -1);
Eloquent::setConnectionResolver(new ConnectionResolver($pdo, $driverName, $tablePrefix));
}
return new BaseSentry($userProvider = new UserProvider(new NativeHasher()), new GroupProvider(), new ThrottleProvider($userProvider), new CISession($ci->session), new CICookie($ci->input), $ci->input->ip_address());
}
示例7: bootModels
/**
* @param $resolver
*/
protected function bootModels($resolver)
{
Model::clearBootedModels();
Model::setEventDispatcher($this->app['illuminate.events']);
Model::setConnectionResolver($resolver);
$this->app['dispatcher']->dispatch(NineEvents::MODELS_BOOTED, new EloquentEvent($this->container->get('db')));
}
示例8: register
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('flarum.db', function () {
$factory = new ConnectionFactory($this->app);
$connection = $factory->make($this->app->make('flarum.config')['database']);
$connection->setEventDispatcher($this->app->make('Illuminate\\Contracts\\Events\\Dispatcher'));
$connection->setFetchMode(PDO::FETCH_CLASS);
return $connection;
});
$this->app->alias('flarum.db', 'Illuminate\\Database\\ConnectionInterface');
$this->app->singleton('Illuminate\\Database\\ConnectionResolverInterface', function () {
$resolver = new ConnectionResolver(['flarum' => $this->app->make('flarum.db')]);
$resolver->setDefaultConnection('flarum');
return $resolver;
});
$this->app->alias('Illuminate\\Database\\ConnectionResolverInterface', 'db');
if (Core::isInstalled()) {
$this->app->booting(function () {
$resolver = $this->app->make('Illuminate\\Database\\ConnectionResolverInterface');
Model::setConnectionResolver($resolver);
Model::setEventDispatcher($this->app->make('events'));
});
}
$this->app->singleton('Flarum\\Migrations\\MigrationRepositoryInterface', function ($app) {
return new DatabaseMigrationRepository($app['db'], 'migrations');
});
}
示例9: addMockConnection
/**
* Set mock connection.
*
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function addMockConnection(Model $model)
{
$resolver = m::mock('\\Illuminate\\Database\\ConnectionResolverInterface');
$model->setConnectionResolver($resolver);
$resolver->shouldReceive('connection')->andReturn(m::mock('\\Illuminate\\Database\\Connection'));
$model->getConnection()->shouldReceive('getQueryGrammar')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Grammars\\Grammar'));
$model->getConnection()->shouldReceive('getPostProcessor')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Processors\\Processor'));
}
示例10: __construct
public function __construct()
{
$settings = array('driver' => 'pgsql', 'host' => 'localhost', 'database' => 'yesbike', 'username' => 'postgres', 'password' => 'macedo', 'charset' => 'UTF8', 'prefix' => '');
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new \Illuminate\Container\Container());
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
}
示例11: setupDatabaseResolver
/**
* Sets up the Eloquent Connection Resolver with the given PDO connection.
*
* @param PDO $pdo
* @param string $driverName
* @param string $tablePrefix
* @return void
*/
public static function setupDatabaseResolver(PDO $pdo, $driverName = null, $tablePrefix = '')
{
// If Eloquent doesn't exist, then we must assume they are using their own providers.
if (class_exists('Illuminate\\Database\\Eloquent\\Model')) {
if (is_null($driverName)) {
$driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
}
Eloquent::setConnectionResolver(new ConnectionResolver($pdo, $driverName, $tablePrefix));
}
}
示例12: setConnectionResolver
private function setConnectionResolver()
{
if ($this->checkTablesExists() && is_null(Model::getConnectionResolver())) {
$res = new ConnectionResolver();
DB::pretend(function ($db) use($res) {
$res->addConnection(config('database.default'), $db);
$res->setDefaultConnection(config('database.default'));
});
Model::setConnectionResolver($res);
}
}
示例13: call
public function call()
{
$container = new Illuminate\Container\Container();
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $connFactory->make(array('driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'flysmuthe', 'username' => \config\SecureConfig::$dbUsername, 'password' => \config\SecureConfig::$dbPassword, 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => ''));
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
//Call next middleware
$this->next->call();
}
示例14: __construct
public function __construct()
{
$this->ci =& get_instance();
$config = $this->ci->db;
// Get the DB object
$pdo = new PDO('mysql:host=' . $config->hostname . ';dbname=' . $config->database, $config->username, $config->password);
$drivers = array('mysql' => '\\Illuminate\\Database\\MySqlConnection', 'pgsql' => '\\Illuminate\\Database\\PostgresConnection', 'sqlite' => '\\Illuminate\\Database\\SQLiteConnection');
$conn = new $drivers['mysql']($pdo, $config->database, $config->dbprefix);
$resolver = new Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
}
示例15: load
public function load()
{
// Database information : should be inject with DI
$settings = array('driver' => 'sqlite', 'database' => 'vdm_posts_db.sqlite', 'prefix' => '');
// Bootstrap Eloquent ORM
$container = new Container();
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
}