Commit 97dbdfc7 authored by Jens Segers's avatar Jens Segers

Connection tweaks, fixes #413

parent ca1a9292
......@@ -143,7 +143,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
$relation = $caller['function'];
}
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
......@@ -234,7 +234,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
{
$relation = $this->getBelongsToManyCaller();
}
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
......@@ -278,7 +278,7 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
// Check the connection type
if ($connection instanceof \Jenssegers\Mongodb\Connection)
{
return new QueryBuilder($connection);
return new QueryBuilder($connection, $connection->getPostProcessor());
}
return parent::newBaseQueryBuilder();
......
<?php namespace Jenssegers\Mongodb;
use Exception;
use MongoCollection;
use Exception, MongoCollection;
use Jenssegers\Mongodb\Connection;
class Collection {
......
<?php namespace Jenssegers\Mongodb;
use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoClient;
class Connection extends \Illuminate\Database\Connection {
......@@ -34,13 +32,25 @@ class Connection extends \Illuminate\Database\Connection {
$dsn = $this->getDsn($config);
// You can pass options directly to the MongoClient constructor
$options = array_get($config, 'options', array());
$options = array_get($config, 'options', []);
// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
// Select database
$this->db = $this->connection->{$config['database']};
$this->useDefaultPostProcessor();
}
/**
* Get the default post processor instance.
*
* @return Query\Processor
*/
protected function getDefaultPostProcessor()
{
return new Query\Processor;
}
/**
......@@ -51,7 +61,9 @@ class Connection extends \Illuminate\Database\Connection {
*/
public function collection($collection)
{
$query = new QueryBuilder($this);
$processor = $this->getPostProcessor();
$query = new Query\Builder($this, $processor);
return $query->from($collection);
}
......@@ -120,12 +132,12 @@ class Connection extends \Illuminate\Database\Connection {
{
// Add credentials as options, this makes sure the connection will not fail if
// the username or password contains strange characters.
if (isset($config['username']) && $config['username'])
if ( ! empty($config['username']))
{
$options['username'] = $config['username'];
}
if (isset($config['password']) && $config['password'])
if ( ! empty($config['password']))
{
$options['password'] = $config['password'];
}
......@@ -156,13 +168,19 @@ class Connection extends \Illuminate\Database\Connection {
// need to establish the MongoClient and return them back for use.
extract($config);
// Check if the user passed a complete dsn to the configuration.
if ( ! empty($dsn))
{
return $dsn;
}
// Treat host option as array of hosts
$hosts = is_array($config['host']) ? $config['host'] : array($config['host']);
$hosts = is_array($host) ? $host : [$host];
// Add ports to hosts
foreach ($hosts as &$host)
{
if (isset($config['port']))
// Check if we need to add a port to the host
if (strpos($host, ':') === false and isset($port))
{
$host = "{$host}:{$port}";
}
......
<?php namespace Jenssegers\Mongodb\Query;
use MongoId;
use MongoRegex;
use MongoDate;
use DateTime;
use Closure;
use Illuminate\Database\Query\Builder as QueryBuilder;
use MongoId, MongoRegex, MongoDate, DateTime, Closure;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Jenssegers\Mongodb\Connection;
class Builder extends QueryBuilder {
class Builder extends BaseBuilder {
/**
* The database collection
......@@ -69,9 +64,10 @@ class Builder extends QueryBuilder {
* @param Connection $connection
* @return void
*/
public function __construct(Connection $connection)
public function __construct(Connection $connection, Processor $processor)
{
$this->connection = $connection;
$this->processor = $processor;
}
/**
......@@ -656,7 +652,7 @@ class Builder extends QueryBuilder {
*/
public function newQuery()
{
return new Builder($this->connection);
return new Builder($this->connection, $this->processor);
}
/**
......@@ -716,12 +712,12 @@ class Builder extends QueryBuilder {
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
$params = func_get_args();
// Remove the leading $ from operators.
if (func_num_args() == 3)
{
$operator = &$params[1];
if (starts_with($operator, '$'))
{
$operator = substr($operator, 1);
......
<?php namespace Jenssegers\Mongodb\Query;
use Illuminate\Database\Query\Processors\Processor as BaseProcessor;
class Processor extends BaseProcessor {
}
......@@ -119,4 +119,15 @@ class ConnectionTest extends TestCase {
$connection = DB::connection('mongodb');
}
public function testHostWithPorts()
{
$hosts = ['localhost:27001', 'localhost:27002'];
Config::set('database.connections.mongodb.port', 27000);
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
$database = Config::get('database.connections.mongodb.database');
$this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
$connection = DB::connection('mongodb');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment