Skip to content

Commit c110d76

Browse files
committed
Small BelongsToMany tweak
1 parent 040b794 commit c110d76

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -200,42 +200,43 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
200200
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
201201
}
202202

203-
/**
204-
* Define a many-to-many relationship.
205-
*
206-
* @param string $related
207-
* @param string $table
208-
* @param string $foreignKey
209-
* @param string $otherKey
210-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
211-
*/
212-
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null)
213-
{
214-
$caller = $this->getBelongsToManyCaller();
215-
216-
// First, we'll need to determine the foreign key and "other key" for the
217-
// relationship. Once we have determined the keys we'll make the query
218-
// instances as well as the relationship instances we need for this.
219-
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
220-
221-
$instance = new $related;
222-
223-
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
224-
// If no table name was provided, we can guess it by concatenating the two
225-
// models using underscores in alphabetical order. The two model names
226-
// are transformed to snake case from their default CamelCase also.
227-
if (is_null($collection))
228-
{
229-
$collection = snake_case(str_plural(class_basename($related)));
230-
}
231-
232-
// Now we're ready to create a new query builder for the related model and
233-
// the relationship instances for the relation. The relations will set
234-
// appropriate query constraint and entirely manages the hydrations.
235-
$query = $instance->newQuery();
236-
237-
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $caller['function']);
238-
}
203+
/**
204+
* Define a many-to-many relationship.
205+
*
206+
* @param string $related
207+
* @param string $table
208+
* @param string $foreignKey
209+
* @param string $otherKey
210+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
211+
*/
212+
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null)
213+
{
214+
$caller = $this->getBelongsToManyCaller();
215+
216+
// First, we'll need to determine the foreign key and "other key" for the
217+
// relationship. Once we have determined the keys we'll make the query
218+
// instances as well as the relationship instances we need for this.
219+
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
220+
221+
$instance = new $related;
222+
223+
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
224+
225+
// If no table name was provided, we can guess it by concatenating the two
226+
// models using underscores in alphabetical order. The two model names
227+
// are transformed to snake case from their default CamelCase also.
228+
if (is_null($collection))
229+
{
230+
$collection = $instance->getTable();
231+
}
232+
233+
// Now we're ready to create a new query builder for the related model and
234+
// the relationship instances for the relation. The relations will set
235+
// appropriate query constraint and entirely manages the hydrations.
236+
$query = $instance->newQuery();
237+
238+
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $caller['function']);
239+
}
239240

240241
/**
241242
* Get a new query builder instance for the connection.
@@ -312,4 +313,4 @@ public function __call($method, $parameters)
312313
return parent::__call($method, $parameters);
313314
}
314315

315-
}
316+
}

0 commit comments

Comments
 (0)