Commit 9737b9bb authored by Jens Segers's avatar Jens Segers

Move hybrid relation logic to its own trait

parent 6460d584
<?php namespace Jenssegers\Eloquent;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Relations\HasOne;
use Jenssegers\Mongodb\Relations\HasMany;
use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Jenssegers\Mongodb\Relations\MorphTo;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
abstract class Model extends \Illuminate\Database\Eloquent\Model {
/**
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hasOne($related, $foreignKey = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::hasOne($related, $foreignKey, $localKey);
}
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
* Define a polymorphic one-to-one relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::morphOne($related, $name, $type, $id, $localKey );
}
$instance = new $related;
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
}
/**
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::hasMany($related, $foreignKey, $localKey);
}
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
* Define a polymorphic one-to-many relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::morphMany($related, $name, $type, $id, $localKey);
}
$instance = new $related;
// Here we will gather up the morph type and ID for the relationship so that we
// can properly query the intermediate table of a relation. Finally, we will
// get the table and create the relationship instances for the developers.
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
}
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if (is_null($relation))
{
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
}
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey))
{
$foreignKey = snake_case($relation).'_id';
}
$instance = new $related;
// Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations.
$query = $instance->newQuery();
$otherKey = $otherKey ?: $instance->getKeyName();
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
}
/**
* Define a polymorphic, inverse one-to-one or many relationship.
*
* @param string $name
* @param string $type
* @param string $id
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function morphTo($name = null, $type = null, $id = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name))
{
list(, $caller) = debug_backtrace(false);
$name = snake_case($caller['function']);
}
list($type, $id) = $this->getMorphs($name, $type, $id);
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type))
{
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
);
}
// If we are not eager loading the relatinship, we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected.
else
{
$instance = new $class;
return new MorphTo(
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
);
}
}
/**
* Define a many-to-many relationship.
*
* @param string $related
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
if (is_null($relation))
{
$relation = $this->getBelongsToManyCaller();
}
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
}
// First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query
// instances as well as the relationship instances we need for this.
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
$instance = new $related;
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
if (is_null($collection))
{
$collection = $instance->getTable();
}
// Now we're ready to create a new query builder for the related model and
// the relationship instances for the relation. The relations will set
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
}
/**
* Get a new query builder instance for the connection.
*
* @return Builder
*/
protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();
// Check the connection type
if ($connection instanceof \Jenssegers\Mongodb\Connection)
{
return new QueryBuilder($connection, $connection->getPostProcessor());
}
return parent::newBaseQueryBuilder();
}
use HybridRelations;
}
......@@ -22,7 +22,6 @@ class Connection extends \Illuminate\Database\Connection {
* Create a new database connection instance.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
......@@ -143,7 +142,7 @@ class Connection extends \Illuminate\Database\Connection {
}
// By default driver options is an empty array.
$driverOptions = array();
$driverOptions = [];
if (isset($config['driver_options']) && is_array($config['driver_options']))
{
......@@ -155,8 +154,6 @@ class Connection extends \Illuminate\Database\Connection {
/**
* Disconnect from the underlying MongoClient connection.
*
* @return void
*/
public function disconnect()
{
......@@ -229,7 +226,7 @@ class Connection extends \Illuminate\Database\Connection {
*/
public function __call($method, $parameters)
{
return call_user_func_array(array($this->db, $method), $parameters);
return call_user_func_array([$this->db, $method], $parameters);
}
}
<?php namespace Jenssegers\Mongodb\Eloquent;
use MongoCursor;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use MongoCursor;
class Builder extends EloquentBuilder {
......@@ -11,10 +11,10 @@ class Builder extends EloquentBuilder {
*
* @var array
*/
protected $passthru = array(
protected $passthru = [
'toSql', 'lists', 'insert', 'insertGetId', 'pluck',
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull',
);
];
/**
* Update a record in the database.
......@@ -104,7 +104,7 @@ class Builder extends EloquentBuilder {
* @param array $extra
* @return int
*/
public function increment($column, $amount = 1, array $extra = array())
public function increment($column, $amount = 1, array $extra = [])
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
......@@ -119,7 +119,7 @@ class Builder extends EloquentBuilder {
$this->model->syncOriginalAttribute($column);
$result = $this->model->update(array($column => $value));
$result = $this->model->update([$column => $value]);
return $result;
}
......@@ -135,7 +135,7 @@ class Builder extends EloquentBuilder {
* @param array $extra
* @return int
*/
public function decrement($column, $amount = 1, array $extra = array())
public function decrement($column, $amount = 1, array $extra = [])
{
// Intercept operations on embedded models and delegate logic
// to the parent relation instance.
......@@ -150,7 +150,7 @@ class Builder extends EloquentBuilder {
$this->model->syncOriginalAttribute($column);
return $this->model->update(array($column => $value));
return $this->model->update([$column => $value]);
}
return parent::decrement($column, $amount, $extra);
......@@ -194,7 +194,7 @@ class Builder extends EloquentBuilder {
});
// If the operator is <, <= or !=, we will use whereNotIn.
$not = in_array($operator, array('<', '<=', '!='));
$not = in_array($operator, ['<', '<=', '!=']);
// If we are comparing to 0, we need an additional $not flip.
if ($count == 0) $not = !$not;
......
......@@ -37,7 +37,7 @@ class Collection extends EloquentCollection {
// and keep going.
if (func_num_args() == 2)
{
list($value, $operator) = array($operator, '=');
list($value, $operator) = [$operator, '='];
}
return $this->filter(function ($item) use ($key, $operator, $value)
......
<?php namespace Jenssegers\Mongodb\Eloquent;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Jenssegers\Mongodb\Relations\HasMany;
use Jenssegers\Mongodb\Relations\HasOne;
use Jenssegers\Mongodb\Relations\MorphTo;
trait HybridRelations {
/**
* Define a one-to-one relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hasOne($related, $foreignKey = null, $localKey = null)
{
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::hasOne($related, $foreignKey, $localKey);
}
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
* Define a polymorphic one-to-one relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::morphOne($related, $name, $type, $id, $localKey );
}
$instance = new $related;
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
}
/**
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
{
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::hasMany($related, $foreignKey, $localKey);
}
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
}
/**
* Define a polymorphic one-to-many relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::morphMany($related, $name, $type, $id, $localKey);
}
$instance = new $related;
// Here we will gather up the morph type and ID for the relationship so that we
// can properly query the intermediate table of a relation. Finally, we will
// get the table and create the relationship instances for the developers.
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
}
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if (is_null($relation))
{
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
}
// If no foreign key was supplied, we can use a backtrace to guess the proper
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey))
{
$foreignKey = snake_case($relation) . '_id';
}
$instance = new $related;
// Once we have the foreign key names, we'll just create a new Eloquent query
// for the related models and returns the relationship instance which will
// actually be responsible for retrieving and hydrating every relations.
$query = $instance->newQuery();
$otherKey = $otherKey ?: $instance->getKeyName();
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
}
/**
* Define a polymorphic, inverse one-to-one or many relationship.
*
* @param string $name
* @param string $type
* @param string $id
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function morphTo($name = null, $type = null, $id = null)
{
// If no name is provided, we will use the backtrace to get the function name
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if (is_null($name))
{
list(, $caller) = debug_backtrace(false);
$name = snake_case($caller['function']);
}
list($type, $id) = $this->getMorphs($name, $type, $id);
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type))
{
return new MorphTo(
$this->newQuery(), $this, $id, null, $type, $name
);
}
// If we are not eager loading the relatinship, we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected.
else
{
$instance = new $class;
return new MorphTo(
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
);
}
}
/**
* Define a many-to-many relationship.
*
* @param string $related
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
if (is_null($relation))
{
$relation = $this->getBelongsToManyCaller();
}
// Check if it is a relation with an original model.
if ($related instanceof Model)
{
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
}
// First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query
// instances as well as the relationship instances we need for this.
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
$instance = new $related;
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
if (is_null($collection))
{
$collection = $instance->getTable();
}
// Now we're ready to create a new query builder for the related model and
// the relationship instances for the relation. The relations will set
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
}
}
......@@ -2,16 +2,21 @@
use Carbon\Carbon;
use DateTime;
use MongoDate;
use MongoId;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Eloquent\Builder;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;
use Jenssegers\Mongodb\Relations\EmbedsOneOrMany;
use MongoDate;
use MongoId;
use ReflectionMethod;
abstract class Model extends \Jenssegers\Eloquent\Model {
abstract class Model extends BaseModel {
use HybridRelations;
/**
* The collection associated with the model.
......@@ -292,7 +297,6 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*
* @param string $key
* @param mixed $value
* @return void
*/
public function setAttribute($key, $value)
{
......@@ -361,7 +365,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*/
public function drop($columns)
{
if ( ! is_array($columns)) $columns = array($columns);
if ( ! is_array($columns)) $columns = [$columns];
// Unset attributes
foreach ($columns as $column)
......@@ -394,7 +398,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
}
// Do batch push by default.
if ( ! is_array($values)) $values = array($values);
if ( ! is_array($values)) $values = [$values];
$query = $this->setKeysForSaveQuery($this->newQuery());
......@@ -416,7 +420,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
public function pull($column, $values)
{
// Do batch pull by default.
if ( ! is_array($values)) $values = array($values);
if ( ! is_array($values)) $values = [$values];
$query = $this->setKeysForSaveQuery($this->newQuery());
......@@ -431,11 +435,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
* @param string $column
* @param array $values
* @param bool $unique
* @return void
*/
protected function pushAttributeValues($column, array $values, $unique = false)
{
$current = $this->getAttributeFromArray($column) ?: array();
$current = $this->getAttributeFromArray($column) ?: [];
foreach ($values as $value)
{
......@@ -455,11 +458,10 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
*
* @param string $column
* @param array $values
* @return void
*/
protected function pullAttributeValues($column, array $values)
{
$current = $this->getAttributeFromArray($column) ?: array();
$current = $this->getAttributeFromArray($column) ?: [];
foreach ($values as $value)
{
......@@ -507,6 +509,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return new Builder($query);
}
/**
* Get a new query builder instance for the connection.
*
* @return Builder
*/
protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();
return new QueryBuilder($connection, $connection->getPostProcessor());
}
/**
* Handle dynamic method calls into the method.
*
......@@ -519,7 +533,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
// Unset method
if ($method == 'unset')
{
return call_user_func_array(array($this, 'drop'), $parameters);
return call_user_func_array([$this, 'drop'], $parameters);
}
return parent::__call($method, $parameters);
......
......@@ -6,8 +6,6 @@ class MongodbServiceProvider extends ServiceProvider {
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
......@@ -18,8 +16,6 @@ class MongodbServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
......
<?php namespace Jenssegers\Mongodb\Query;
use MongoId;
use MongoRegex;
use MongoDate;
use DateTime;
use Closure;
use DateTime;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Connection;
use MongoDate;
use MongoId;
use MongoRegex;
class Builder extends BaseBuilder {
......@@ -52,7 +52,7 @@ class Builder extends BaseBuilder {
*
* @var array
*/
protected $operators = array(
protected $operators = [
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'not like', 'between', 'ilike',
'&', '|', '^', '<<', '>>',
......@@ -60,14 +60,14 @@ class Builder extends BaseBuilder {
'exists', 'type', 'mod', 'where', 'all', 'size', 'regex', 'text', 'slice', 'elemmatch',
'geowithin', 'geointersects', 'near', 'nearsphere', 'geometry',
'maxdistance', 'center', 'centersphere', 'box', 'polygon', 'uniquedocs',
);
];
/**
* Operator conversion.
*
* @var array
*/
protected $conversion = array(
protected $conversion = [
'=' => '=',
'!=' => '$ne',
'<>' => '$ne',
......@@ -75,13 +75,12 @@ class Builder extends BaseBuilder {
'<=' => '$lte',
'>' => '$gt',
'>=' => '$gte',
);
];
/**
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
public function __construct(Connection $connection, Processor $processor)
{
......@@ -135,7 +134,7 @@ class Builder extends BaseBuilder {
* @param array $columns
* @return mixed
*/
public function find($id, $columns = array())
public function find($id, $columns = [])
{
return $this->where('_id', '=', $this->convertKey($id))->first($columns);
}
......@@ -146,7 +145,7 @@ class Builder extends BaseBuilder {
* @param array $columns
* @return array|static[]
*/
public function get($columns = array())
public function get($columns = [])
{
return parent::get($columns);
}
......@@ -157,7 +156,7 @@ class Builder extends BaseBuilder {
* @param array $columns
* @return array|static[]
*/
public function getFresh($columns = array())
public function getFresh($columns = [])
{
// If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving
......@@ -165,7 +164,7 @@ class Builder extends BaseBuilder {
if (is_null($this->columns)) $this->columns = $columns;
// Drop all columns if * is present, MongoDB does not work this way.
if (in_array('*', $this->columns)) $this->columns = array();
if (in_array('*', $this->columns)) $this->columns = [];
// Compile wheres
$wheres = $this->compileWheres();
......@@ -173,7 +172,7 @@ class Builder extends BaseBuilder {
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
if ($this->groups or $this->aggregate or $this->paginating)
{
$group = array();
$group = [];
// Add grouping columns to the $group part of the aggregation pipeline.
if ($this->groups)
......@@ -184,7 +183,7 @@ class Builder extends BaseBuilder {
// When grouping, also add the $last operator to each grouped field,
// this mimics MySQL's behaviour a bit.
$group[$column] = array('$last' => '$' . $column);
$group[$column] = ['$last' => '$' . $column];
}
// Do the same for other columns that are selected.
......@@ -192,7 +191,7 @@ class Builder extends BaseBuilder {
{
$key = str_replace('.', '_', $column);
$group[$key] = array('$last' => '$' . $column);
$group[$key] = ['$last' => '$' . $column];
}
}
......@@ -207,12 +206,12 @@ class Builder extends BaseBuilder {
// Translate count into sum.
if ($function == 'count')
{
$group['aggregate'] = array('$sum' => 1);
$group['aggregate'] = ['$sum' => 1];
}
// Pass other functions directly.
else
{
$group['aggregate'] = array('$' . $function => '$' . $column);
$group['aggregate'] = ['$' . $function => '$' . $column];
}
}
}
......@@ -234,15 +233,15 @@ class Builder extends BaseBuilder {
}
// Build the aggregation pipeline.
$pipeline = array();
if ($wheres) $pipeline[] = array('$match' => $wheres);
if ($group) $pipeline[] = array('$group' => $group);
$pipeline = [];
if ($wheres) $pipeline[] = ['$match' => $wheres];
if ($group) $pipeline[] = ['$group' => $group];
// Apply order and limit
if ($this->orders) $pipeline[] = array('$sort' => $this->orders);
if ($this->offset) $pipeline[] = array('$skip' => $this->offset);
if ($this->limit) $pipeline[] = array('$limit' => $this->limit);
if ($this->projections) $pipeline[] = array('$project' => $this->projections);
if ($this->orders) $pipeline[] = ['$sort' => $this->orders];
if ($this->offset) $pipeline[] = ['$skip' => $this->offset];
if ($this->limit) $pipeline[] = ['$limit' => $this->limit];
if ($this->projections) $pipeline[] = ['$project' => $this->projections];
// Execute aggregation
$results = $this->collection->aggregate($pipeline);
......@@ -273,7 +272,7 @@ class Builder extends BaseBuilder {
// Normal query
else
{
$columns = array();
$columns = [];
// Convert select columns to simple projections.
foreach ($this->columns as $column)
......@@ -309,7 +308,7 @@ class Builder extends BaseBuilder {
*/
public function generateCacheKey()
{
$key = array(
$key = [
'connection' => $this->connection->getName(),
'collection' => $this->collection->getName(),
'wheres' => $this->wheres,
......@@ -319,7 +318,7 @@ class Builder extends BaseBuilder {
'offset' => $this->offset,
'limit' => $this->limit,
'aggregate' => $this->aggregate,
);
];
return md5(serialize(array_values($key)));
}
......@@ -331,7 +330,7 @@ class Builder extends BaseBuilder {
* @param array $columns
* @return mixed
*/
public function aggregate($function, $columns = array())
public function aggregate($function, $columns = [])
{
$this->aggregate = compact('function', 'columns');
......@@ -361,7 +360,7 @@ class Builder extends BaseBuilder {
if ($column)
{
$this->columns = array($column);
$this->columns = [$column];
}
return $this;
......@@ -444,7 +443,7 @@ class Builder extends BaseBuilder {
}
}
if ( ! $batch) $values = array($values);
if ( ! $batch) $values = [$values];
// Batch insert
$result = $this->collection->batchInsert($values);
......@@ -482,12 +481,12 @@ class Builder extends BaseBuilder {
* @param array $options
* @return int
*/
public function update(array $values, array $options = array())
public function update(array $values, array $options = [])
{
// Use $set as default operator.
if ( ! starts_with(key($values), '$'))
{
$values = array('$set' => $values);
$values = ['$set' => $values];
}
return $this->performUpdate($values, $options);
......@@ -501,9 +500,9 @@ class Builder extends BaseBuilder {
* @param array $extra
* @return int
*/
public function increment($column, $amount = 1, array $extra = array(), array $options = array())
public function increment($column, $amount = 1, array $extra = [], array $options = [])
{
$query = array('$inc' => array($column => $amount));
$query = ['$inc' => [$column => $amount]];
if ( ! empty($extra))
{
......@@ -529,7 +528,7 @@ class Builder extends BaseBuilder {
* @param array $extra
* @return int
*/
public function decrement($column, $amount = 1, array $extra = array(), array $options = array())
public function decrement($column, $amount = 1, array $extra = [], array $options = [])
{
return $this->increment($column, -1 * $amount, $extra, $options);
}
......@@ -542,7 +541,7 @@ class Builder extends BaseBuilder {
*/
public function pluck($column)
{
$result = (array) $this->first(array($column));
$result = (array) $this->first([$column]);
// MongoDB returns the _id field even if you did not ask for it, so we need to
// remove this from the result.
......@@ -592,8 +591,6 @@ class Builder extends BaseBuilder {
/**
* Run a truncate statement on the table.
*
* @return void
*/
public function truncate()
{
......@@ -670,15 +667,15 @@ class Builder extends BaseBuilder {
if (is_array($column))
{
$query = array($operator => $column);
$query = [$operator => $column];
}
elseif ($batch)
{
$query = array($operator => array($column => array('$each' => $value)));
$query = [$operator => [$column => ['$each' => $value]]];
}
else
{
$query = array($operator => array($column => $value));
$query = [$operator => [$column => $value]];
}
return $this->performUpdate($query);
......@@ -701,11 +698,11 @@ class Builder extends BaseBuilder {
if (is_array($column))
{
$query = array($operator => $column);
$query = [$operator => $column];
}
else
{
$query = array($operator => array($column => $value));
$query = [$operator => [$column => $value]];
}
return $this->performUpdate($query);
......@@ -719,16 +716,16 @@ class Builder extends BaseBuilder {
*/
public function drop($columns)
{
if ( ! is_array($columns)) $columns = array($columns);
if ( ! is_array($columns)) $columns = [$columns];
$fields = array();
$fields = [];
foreach ($columns as $column)
{
$fields[$column] = 1;
}
$query = array('$unset' => $fields);
$query = ['$unset' => $fields];
return $this->performUpdate($query);
}
......@@ -750,7 +747,7 @@ class Builder extends BaseBuilder {
* @param array $options
* @return int
*/
protected function performUpdate($query, array $options = array())
protected function performUpdate($query, array $options = [])
{
// Update multiple items by default.
if ( ! array_key_exists('multiple', $options))
......@@ -823,10 +820,10 @@ class Builder extends BaseBuilder {
protected function compileWheres()
{
// The wheres to compile.
$wheres = $this->wheres ?: array();
$wheres = $this->wheres ?: [];
// We will add all compiled wheres to this array.
$compiled = array();
$compiled = [];
foreach ($wheres as $i => &$where)
{
......@@ -836,7 +833,7 @@ class Builder extends BaseBuilder {
$where['operator'] = strtolower($where['operator']);
// Operator conversions
$convert = array(
$convert = [
'regexp' => 'regex',
'elemmatch' => 'elemMatch',
'geointersects' => 'geoIntersects',
......@@ -845,7 +842,7 @@ class Builder extends BaseBuilder {
'maxdistance' => 'maxDistance',
'centersphere' => 'centerSphere',
'uniquedocs' => 'uniqueDocs',
);
];
if (array_key_exists($where['operator'], $convert))
{
......@@ -893,14 +890,14 @@ class Builder extends BaseBuilder {
// Wrap the where with an $or operator.
if ($where['boolean'] == 'or')
{
$result = array('$or' => array($result));
$result = ['$or' => [$result]];
}
// If there are multiple wheres, we will wrap it with $and. This is needed
// to make nested wheres work.
elseif (count($wheres) > 1)
{
$result = array('$and' => array($result));
$result = ['$and' => [$result]];
}
// Merge the compiled where with the others.
......@@ -928,7 +925,7 @@ class Builder extends BaseBuilder {
}
// Manipulate regexp operations.
elseif (in_array($operator, array('regexp', 'not regexp', 'regex', 'not regex')))
elseif (in_array($operator, ['regexp', 'not regexp', 'regex', 'not regex']))
{
// Automatically convert regular expression strings to MongoRegex objects.
if ( ! $value instanceof MongoRegex)
......@@ -946,15 +943,15 @@ class Builder extends BaseBuilder {
if ( ! isset($operator) or $operator == '=')
{
$query = array($column => $value);
$query = [$column => $value];
}
elseif (array_key_exists($operator, $this->conversion))
{
$query = array($column => array($this->conversion[$operator] => $value));
$query = [$column => [$this->conversion[$operator] => $value]];
}
else
{
$query = array($column => array('$' . $operator => $value));
$query = [$column => ['$' . $operator => $value]];
}
return $query;
......@@ -971,14 +968,14 @@ class Builder extends BaseBuilder {
{
extract($where);
return array($column => array('$in' => array_values($values)));
return [$column => ['$in' => array_values($values)]];
}
protected function compileWhereNotIn($where)
{
extract($where);
return array($column => array('$nin' => array_values($values)));
return [$column => ['$nin' => array_values($values)]];
}
protected function compileWhereNull($where)
......@@ -1003,29 +1000,29 @@ class Builder extends BaseBuilder {
if ($not)
{
return array(
'$or' => array(
array(
$column => array(
return [
'$or' => [
[
$column => [
'$lte' => $values[0],
),
),
array(
$column => array(
],
],
[
$column => [
'$gte' => $values[1],
),
),
),
);
],
],
],
];
}
else
{
return array(
$column => array(
return [
$column => [
'$gte' => $values[0],
'$lte' => $values[1],
),
);
],
];
}
}
......@@ -1045,7 +1042,7 @@ class Builder extends BaseBuilder {
{
if ($method == 'unset')
{
return call_user_func_array(array($this, 'drop'), $parameters);
return call_user_func_array([$this, 'drop'], $parameters);
}
return parent::__call($method, $parameters);
......
......@@ -4,8 +4,6 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
......@@ -22,7 +20,6 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
......
<?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
class BelongsToMany extends EloquentBelongsToMany {
......@@ -10,7 +10,6 @@ class BelongsToMany extends EloquentBelongsToMany {
* Hydrate the pivot table relationship on the models.
*
* @param array $models
* @return void
*/
protected function hydratePivotRelation(array $models)
{
......@@ -23,15 +22,13 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param array $columns
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
protected function getSelectColumns(array $columns = array('*'))
protected function getSelectColumns(array $columns = ['*'])
{
return $columns;
}
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
......@@ -60,9 +57,9 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public function save(Model $model, array $joining = array(), $touch = true)
public function save(Model $model, array $joining = [], $touch = true)
{
$model->save(array('touch' => false));
$model->save(['touch' => false]);
$this->attach($model, $joining, $touch);
......@@ -77,14 +74,14 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $attributes, array $joining = array(), $touch = true)
public function create(array $attributes, array $joining = [], $touch = true)
{
$instance = $this->related->newInstance($attributes);
// Once we save the related model, we need to attach it to the base model via
// through intermediate table so we'll use the existing "attach" method to
// accomplish this which will insert the record and any more attributes.
$instance->save(array('touch' => false));
$instance->save(['touch' => false]);
$this->attach($instance, $joining, $touch);
......@@ -100,16 +97,16 @@ class BelongsToMany extends EloquentBelongsToMany {
*/
public function sync($ids, $detaching = true)
{
$changes = array(
'attached' => array(), 'detached' => array(), 'updated' => array(),
);
$changes = [
'attached' => [], 'detached' => [], 'updated' => [],
];
if ($ids instanceof Collection) $ids = $ids->modelKeys();
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->parent->{$this->otherKey} ?: array();
$current = $this->parent->{$this->otherKey} ?: [];
// See issue #256.
if ($current instanceof Collection) $current = $ids->modelKeys();
......@@ -153,7 +150,6 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param mixed $id
* @param array $attributes
* @param bool $touch
* @return void
*/
public function updateExistingPivot($id, array $attributes, $touch = true)
{
......@@ -166,9 +162,8 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param mixed $id
* @param array $attributes
* @param bool $touch
* @return void
*/
public function attach($id, array $attributes = array(), $touch = true)
public function attach($id, array $attributes = [], $touch = true)
{
if ($id instanceof Model)
{
......@@ -202,7 +197,7 @@ class BelongsToMany extends EloquentBelongsToMany {
* @param bool $touch
* @return int
*/
public function detach($ids = array(), $touch = true)
public function detach($ids = [], $touch = true)
{
if ($ids instanceof Model) $ids = (array) $ids->getKey();
......@@ -243,7 +238,7 @@ class BelongsToMany extends EloquentBelongsToMany {
// First we will build a dictionary of child models keyed by the foreign key
// of the relation so that we will easily and quickly match them to their
// parents without having a possibly slow inner loops for every models.
$dictionary = array();
$dictionary = [];
foreach ($results as $result)
{
......
......@@ -99,7 +99,7 @@ class EmbedsMany extends EmbedsOneOrMany {
// Get the correct foreign key value.
$foreignKey = $this->getForeignKeyValue($model);
$result = $this->getBaseQuery()->pull($this->localKey, array($model->getKeyName() => $foreignKey));
$result = $this->getBaseQuery()->pull($this->localKey, [$model->getKeyName() => $foreignKey]);
if ($result) $this->dissociate($model);
......@@ -130,7 +130,7 @@ class EmbedsMany extends EmbedsOneOrMany {
* @param mixed $ids
* @return int
*/
public function dissociate($ids = array())
public function dissociate($ids = [])
{
$ids = $this->getIdsArrayFrom($ids);
......@@ -161,7 +161,7 @@ class EmbedsMany extends EmbedsOneOrMany {
* @param mixed $ids
* @return int
*/
public function destroy($ids = array())
public function destroy($ids = [])
{
$count = 0;
......@@ -187,9 +187,9 @@ class EmbedsMany extends EmbedsOneOrMany {
public function delete()
{
// Overwrite the local key with an empty array.
$result = $this->query->update(array($this->localKey => array()));
$result = $this->query->update([$this->localKey => []]);
if ($result) $this->setEmbedded(array());
if ($result) $this->setEmbedded([]);
return $result;
}
......@@ -200,7 +200,7 @@ class EmbedsMany extends EmbedsOneOrMany {
* @param mixed $ids
* @return int
*/
public function detach($ids = array())
public function detach($ids = [])
{
return $this->destroy($ids);
}
......@@ -296,18 +296,17 @@ class EmbedsMany extends EmbedsOneOrMany {
*/
protected function getEmbedded()
{
return parent::getEmbedded() ?: array();
return parent::getEmbedded() ?: [];
}
/**
* Set the embedded records array.
*
* @param array $models
* @return void
*/
protected function setEmbedded($models)
{
if ( ! is_array($models)) $models = array($models);
if ( ! is_array($models)) $models = [$models];
return parent::setEmbedded(array_values($models));
}
......@@ -324,7 +323,7 @@ class EmbedsMany extends EmbedsOneOrMany {
// Collection methods
if (method_exists('Jenssegers\Mongodb\Eloquent\Collection', $method))
{
return call_user_func_array(array($this->getResults(), $method), $parameters);
return call_user_func_array([$this->getResults(), $method], $parameters);
}
return parent::__call($method, $parameters);
......
......@@ -37,7 +37,7 @@ class EmbedsOne extends EmbedsOneOrMany {
return $this->parent->save();
}
$result = $this->getBaseQuery()->update(array($this->localKey => $model->getAttributes()));
$result = $this->getBaseQuery()->update([$this->localKey => $model->getAttributes()]);
// Attach the model to its parent.
if ($result) $this->associate($model);
......@@ -88,7 +88,7 @@ class EmbedsOne extends EmbedsOneOrMany {
}
// Overwrite the local key with an empty array.
$result = $this->getBaseQuery()->update(array($this->localKey => null));
$result = $this->getBaseQuery()->update([$this->localKey => null]);
// Detach the model from its parent.
if ($result) $this->dissociate();
......
......@@ -59,8 +59,6 @@ abstract class EmbedsOneOrMany extends Relation {
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
......@@ -74,7 +72,6 @@ abstract class EmbedsOneOrMany extends Relation {
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
......@@ -86,7 +83,6 @@ abstract class EmbedsOneOrMany extends Relation {
*
* @param array $models
* @param string $relation
* @return void
*/
public function initRelation(array $models, $relation)
{
......@@ -166,7 +162,7 @@ abstract class EmbedsOneOrMany extends Relation {
foreach ($models as $model) {
$this->save($model);
}
return $models;
}
......@@ -198,7 +194,7 @@ abstract class EmbedsOneOrMany extends Relation {
*/
public function createMany(array $records)
{
$instances = array();
$instances = [];
foreach ($records as $record)
{
......@@ -216,7 +212,7 @@ abstract class EmbedsOneOrMany extends Relation {
*/
protected function getIdsArrayFrom($ids)
{
if ( ! is_array($ids)) $ids = array($ids);
if ( ! is_array($ids)) $ids = [$ids];
foreach ($ids as &$id)
{
......@@ -281,9 +277,9 @@ abstract class EmbedsOneOrMany extends Relation {
* @param array $records
* @return \Jenssegers\Mongodb\Eloquent\Collection
*/
protected function toCollection(array $records = array())
protected function toCollection(array $records = [])
{
$models = array();
$models = [];
foreach ($records as $attributes)
{
......@@ -304,7 +300,7 @@ abstract class EmbedsOneOrMany extends Relation {
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
protected function toModel($attributes = array())
protected function toModel($attributes = [])
{
if (is_null($attributes)) return;
......@@ -315,7 +311,7 @@ abstract class EmbedsOneOrMany extends Relation {
$model->setRelation($this->foreignKey, $this->parent);
// If you remove this, you will get segmentation faults!
$model->setHidden(array_merge($model->getHidden(), array($this->foreignKey)));
$model->setHidden(array_merge($model->getHidden(), [$this->foreignKey]));
return $model;
}
......@@ -357,7 +353,7 @@ abstract class EmbedsOneOrMany extends Relation {
/**
* Check if this relation is nested in another relation.
*
* @return boolean
* @return bool
*/
protected function isNested()
{
......
......@@ -6,8 +6,6 @@ class MorphTo extends EloquentMorphTo {
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
......
......@@ -24,14 +24,13 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
*
* @var array
*/
protected $columns = array();
protected $columns = [];
/**
* Create a new schema blueprint.
*
* @param string $table
* @param Closure $callback
* @return void
*/
public function __construct(Connection $connection, $collection)
{
......@@ -47,7 +46,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
* @param array $options
* @return Blueprint
*/
public function index($columns = null, $options = array())
public function index($columns = null, $options = [])
{
$columns = $this->fluent($columns);
......@@ -55,7 +54,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
if (is_array($columns) && is_int(key($columns)))
{
// Transform the columns to the required array format.
$transform = array();
$transform = [];
foreach ($columns as $column)
{
......@@ -77,7 +76,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
* @param array $options
* @return \Illuminate\Support\Fluent
*/
public function primary($columns = null, $options = array())
public function primary($columns = null, $options = [])
{
return $this->unique($columns, $options);
}
......@@ -96,7 +95,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
if (is_array($columns) && is_int(key($columns)))
{
// Transform the columns to the required array format.
$transform = array();
$transform = [];
foreach ($columns as $column)
{
......@@ -121,7 +120,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$columns = $this->fluent($columns);
$this->index($columns, array('unique' => true));
$this->index($columns, ['unique' => true]);
return $this;
}
......@@ -136,7 +135,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$columns = $this->fluent($columns);
$this->index($columns, array('background' => true));
$this->index($columns, ['background' => true]);
return $this;
}
......@@ -150,7 +149,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
public function sparse($columns = null)
{
$columns = $this->fluent($columns);
$this->index($columns, array('sparse' => true));
$this->index($columns, ['sparse' => true]);
return $this;
}
......@@ -167,7 +166,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
{
$columns = $this->fluent($columns);
$this->index($columns, array('expireAfterSeconds' => $seconds));
$this->index($columns, ['expireAfterSeconds' => $seconds]);
return $this;
}
......@@ -205,7 +204,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
* @param array $parameters
* @return Blueprint
*/
protected function addColumn($type, $name, array $parameters = array())
protected function addColumn($type, $name, array $parameters = [])
{
$this->fluent($name);
......@@ -226,7 +225,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
}
elseif (is_string($columns))
{
return $this->columns = array($columns);
return $this->columns = [$columns];
}
else
{
......
......@@ -14,7 +14,7 @@ class Builder extends \Illuminate\Database\Schema\Builder {
{
$this->connection = $connection;
}
/**
* Determine if the given table has a given column.
*
......
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