Skip to content

Commit 9737b9b

Browse files
committed
Move hybrid relation logic to its own trait
1 parent 6460d58 commit 9737b9b

16 files changed

+426
-448
lines changed

src/Jenssegers/Eloquent/Model.php

Lines changed: 2 additions & 280 deletions
Original file line numberDiff line numberDiff line change
@@ -1,287 +1,9 @@
11
<?php namespace Jenssegers\Eloquent;
22

3-
use Illuminate\Database\Eloquent\Relations\MorphOne;
4-
use Illuminate\Database\Eloquent\Relations\MorphMany;
5-
use Illuminate\Database\Eloquent\Relations\Relation;
6-
use Jenssegers\Mongodb\Relations\HasOne;
7-
use Jenssegers\Mongodb\Relations\HasMany;
8-
use Jenssegers\Mongodb\Relations\BelongsTo;
9-
use Jenssegers\Mongodb\Relations\BelongsToMany;
10-
use Jenssegers\Mongodb\Relations\MorphTo;
11-
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
3+
use Jenssegers\Mongodb\Eloquent\HybridRelations;
124

135
abstract class Model extends \Illuminate\Database\Eloquent\Model {
146

15-
/**
16-
* Define a one-to-one relationship.
17-
*
18-
* @param string $related
19-
* @param string $foreignKey
20-
* @param string $localKey
21-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
22-
*/
23-
public function hasOne($related, $foreignKey = null, $localKey = null)
24-
{
25-
// Check if it is a relation with an original model.
26-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
27-
{
28-
return parent::hasOne($related, $foreignKey, $localKey);
29-
}
30-
31-
$foreignKey = $foreignKey ?: $this->getForeignKey();
32-
33-
$instance = new $related;
34-
35-
$localKey = $localKey ?: $this->getKeyName();
36-
37-
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
38-
}
39-
40-
/**
41-
* Define a polymorphic one-to-one relationship.
42-
*
43-
* @param string $related
44-
* @param string $name
45-
* @param string $type
46-
* @param string $id
47-
* @param string $localKey
48-
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
49-
*/
50-
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
51-
{
52-
// Check if it is a relation with an original model.
53-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
54-
{
55-
return parent::morphOne($related, $name, $type, $id, $localKey );
56-
}
57-
58-
$instance = new $related;
59-
60-
list($type, $id) = $this->getMorphs($name, $type, $id);
61-
62-
$table = $instance->getTable();
63-
64-
$localKey = $localKey ?: $this->getKeyName();
65-
66-
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
67-
}
68-
69-
/**
70-
* Define a one-to-many relationship.
71-
*
72-
* @param string $related
73-
* @param string $foreignKey
74-
* @param string $localKey
75-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
76-
*/
77-
public function hasMany($related, $foreignKey = null, $localKey = null)
78-
{
79-
// Check if it is a relation with an original model.
80-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
81-
{
82-
return parent::hasMany($related, $foreignKey, $localKey);
83-
}
84-
85-
$foreignKey = $foreignKey ?: $this->getForeignKey();
86-
87-
$instance = new $related;
88-
89-
$localKey = $localKey ?: $this->getKeyName();
90-
91-
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
92-
}
93-
94-
/**
95-
* Define a polymorphic one-to-many relationship.
96-
*
97-
* @param string $related
98-
* @param string $name
99-
* @param string $type
100-
* @param string $id
101-
* @param string $localKey
102-
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
103-
*/
104-
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
105-
{
106-
// Check if it is a relation with an original model.
107-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
108-
{
109-
return parent::morphMany($related, $name, $type, $id, $localKey);
110-
}
111-
112-
$instance = new $related;
113-
114-
// Here we will gather up the morph type and ID for the relationship so that we
115-
// can properly query the intermediate table of a relation. Finally, we will
116-
// get the table and create the relationship instances for the developers.
117-
list($type, $id) = $this->getMorphs($name, $type, $id);
118-
119-
$table = $instance->getTable();
120-
121-
$localKey = $localKey ?: $this->getKeyName();
122-
123-
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
124-
}
125-
126-
/**
127-
* Define an inverse one-to-one or many relationship.
128-
*
129-
* @param string $related
130-
* @param string $foreignKey
131-
* @param string $otherKey
132-
* @param string $relation
133-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
134-
*/
135-
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
136-
{
137-
// If no relation name was given, we will use this debug backtrace to extract
138-
// the calling method's name and use that as the relationship name as most
139-
// of the time this will be what we desire to use for the relatinoships.
140-
if (is_null($relation))
141-
{
142-
list(, $caller) = debug_backtrace(false);
143-
144-
$relation = $caller['function'];
145-
}
146-
147-
// Check if it is a relation with an original model.
148-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
149-
{
150-
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
151-
}
152-
153-
// If no foreign key was supplied, we can use a backtrace to guess the proper
154-
// foreign key name by using the name of the relationship function, which
155-
// when combined with an "_id" should conventionally match the columns.
156-
if (is_null($foreignKey))
157-
{
158-
$foreignKey = snake_case($relation).'_id';
159-
}
160-
161-
$instance = new $related;
162-
163-
// Once we have the foreign key names, we'll just create a new Eloquent query
164-
// for the related models and returns the relationship instance which will
165-
// actually be responsible for retrieving and hydrating every relations.
166-
$query = $instance->newQuery();
167-
168-
$otherKey = $otherKey ?: $instance->getKeyName();
169-
170-
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
171-
}
172-
173-
/**
174-
* Define a polymorphic, inverse one-to-one or many relationship.
175-
*
176-
* @param string $name
177-
* @param string $type
178-
* @param string $id
179-
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
180-
*/
181-
public function morphTo($name = null, $type = null, $id = null)
182-
{
183-
// If no name is provided, we will use the backtrace to get the function name
184-
// since that is most likely the name of the polymorphic interface. We can
185-
// use that to get both the class and foreign key that will be utilized.
186-
if (is_null($name))
187-
{
188-
list(, $caller) = debug_backtrace(false);
189-
190-
$name = snake_case($caller['function']);
191-
}
192-
193-
list($type, $id) = $this->getMorphs($name, $type, $id);
194-
195-
// If the type value is null it is probably safe to assume we're eager loading
196-
// the relationship. When that is the case we will pass in a dummy query as
197-
// there are multiple types in the morph and we can't use single queries.
198-
if (is_null($class = $this->$type))
199-
{
200-
return new MorphTo(
201-
$this->newQuery(), $this, $id, null, $type, $name
202-
);
203-
}
204-
205-
// If we are not eager loading the relatinship, we will essentially treat this
206-
// as a belongs-to style relationship since morph-to extends that class and
207-
// we will pass in the appropriate values so that it behaves as expected.
208-
else
209-
{
210-
$instance = new $class;
211-
212-
return new MorphTo(
213-
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
214-
);
215-
}
216-
}
217-
218-
/**
219-
* Define a many-to-many relationship.
220-
*
221-
* @param string $related
222-
* @param string $collection
223-
* @param string $foreignKey
224-
* @param string $otherKey
225-
* @param string $relation
226-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
227-
*/
228-
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
229-
{
230-
// If no relationship name was passed, we will pull backtraces to get the
231-
// name of the calling function. We will use that function name as the
232-
// title of this relation since that is a great convention to apply.
233-
if (is_null($relation))
234-
{
235-
$relation = $this->getBelongsToManyCaller();
236-
}
237-
238-
// Check if it is a relation with an original model.
239-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
240-
{
241-
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
242-
}
243-
244-
// First, we'll need to determine the foreign key and "other key" for the
245-
// relationship. Once we have determined the keys we'll make the query
246-
// instances as well as the relationship instances we need for this.
247-
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
248-
249-
$instance = new $related;
250-
251-
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
252-
253-
// If no table name was provided, we can guess it by concatenating the two
254-
// models using underscores in alphabetical order. The two model names
255-
// are transformed to snake case from their default CamelCase also.
256-
if (is_null($collection))
257-
{
258-
$collection = $instance->getTable();
259-
}
260-
261-
// Now we're ready to create a new query builder for the related model and
262-
// the relationship instances for the relation. The relations will set
263-
// appropriate query constraint and entirely manages the hydrations.
264-
$query = $instance->newQuery();
265-
266-
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
267-
}
268-
269-
/**
270-
* Get a new query builder instance for the connection.
271-
*
272-
* @return Builder
273-
*/
274-
protected function newBaseQueryBuilder()
275-
{
276-
$connection = $this->getConnection();
277-
278-
// Check the connection type
279-
if ($connection instanceof \Jenssegers\Mongodb\Connection)
280-
{
281-
return new QueryBuilder($connection, $connection->getPostProcessor());
282-
}
283-
284-
return parent::newBaseQueryBuilder();
285-
}
7+
use HybridRelations;
2868

2879
}

src/Jenssegers/Mongodb/Connection.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class Connection extends \Illuminate\Database\Connection {
2222
* Create a new database connection instance.
2323
*
2424
* @param array $config
25-
* @return void
2625
*/
2726
public function __construct(array $config)
2827
{
@@ -143,7 +142,7 @@ protected function createConnection($dsn, array $config, array $options)
143142
}
144143

145144
// By default driver options is an empty array.
146-
$driverOptions = array();
145+
$driverOptions = [];
147146

148147
if (isset($config['driver_options']) && is_array($config['driver_options']))
149148
{
@@ -155,8 +154,6 @@ protected function createConnection($dsn, array $config, array $options)
155154

156155
/**
157156
* Disconnect from the underlying MongoClient connection.
158-
*
159-
* @return void
160157
*/
161158
public function disconnect()
162159
{
@@ -229,7 +226,7 @@ public function getDriverName()
229226
*/
230227
public function __call($method, $parameters)
231228
{
232-
return call_user_func_array(array($this->db, $method), $parameters);
229+
return call_user_func_array([$this->db, $method], $parameters);
233230
}
234231

235232
}

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace Jenssegers\Mongodb\Eloquent;
22

3-
use MongoCursor;
4-
use Illuminate\Database\Eloquent\Relations\Relation;
53
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
4+
use Illuminate\Database\Eloquent\Relations\Relation;
5+
use MongoCursor;
66

77
class Builder extends EloquentBuilder {
88

@@ -11,10 +11,10 @@ class Builder extends EloquentBuilder {
1111
*
1212
* @var array
1313
*/
14-
protected $passthru = array(
14+
protected $passthru = [
1515
'toSql', 'lists', 'insert', 'insertGetId', 'pluck',
1616
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull',
17-
);
17+
];
1818

1919
/**
2020
* Update a record in the database.
@@ -104,7 +104,7 @@ public function delete()
104104
* @param array $extra
105105
* @return int
106106
*/
107-
public function increment($column, $amount = 1, array $extra = array())
107+
public function increment($column, $amount = 1, array $extra = [])
108108
{
109109
// Intercept operations on embedded models and delegate logic
110110
// to the parent relation instance.
@@ -119,7 +119,7 @@ public function increment($column, $amount = 1, array $extra = array())
119119

120120
$this->model->syncOriginalAttribute($column);
121121

122-
$result = $this->model->update(array($column => $value));
122+
$result = $this->model->update([$column => $value]);
123123

124124
return $result;
125125
}
@@ -135,7 +135,7 @@ public function increment($column, $amount = 1, array $extra = array())
135135
* @param array $extra
136136
* @return int
137137
*/
138-
public function decrement($column, $amount = 1, array $extra = array())
138+
public function decrement($column, $amount = 1, array $extra = [])
139139
{
140140
// Intercept operations on embedded models and delegate logic
141141
// to the parent relation instance.
@@ -150,7 +150,7 @@ public function decrement($column, $amount = 1, array $extra = array())
150150

151151
$this->model->syncOriginalAttribute($column);
152152

153-
return $this->model->update(array($column => $value));
153+
return $this->model->update([$column => $value]);
154154
}
155155

156156
return parent::decrement($column, $amount, $extra);
@@ -194,7 +194,7 @@ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $o
194194
});
195195

196196
// If the operator is <, <= or !=, we will use whereNotIn.
197-
$not = in_array($operator, array('<', '<=', '!='));
197+
$not = in_array($operator, ['<', '<=', '!=']);
198198

199199
// If we are comparing to 0, we need an additional $not flip.
200200
if ($count == 0) $not = !$not;

0 commit comments

Comments
 (0)