Commit 9cee4693 authored by Taylor Otwell's avatar Taylor Otwell

added support for overriding many-to-many table names.

parent ae711416
...@@ -164,11 +164,12 @@ abstract class Eloquent { ...@@ -164,11 +164,12 @@ abstract class Eloquent {
* Retrieve the query for a *:* relationship. * Retrieve the query for a *:* relationship.
* *
* @param string $model * @param string $model
* @param string $table
* @return mixed * @return mixed
*/ */
public function has_many_and_belongs_to($model) public function has_many_and_belongs_to($model, $table = null)
{ {
return Eloquent\Relate::has_many_and_belongs_to($model, $this); return Eloquent\Relate::has_many_and_belongs_to($model, $table, $this);
} }
/** /**
......
...@@ -64,18 +64,27 @@ class Relate { ...@@ -64,18 +64,27 @@ class Relate {
* Retrieve the query for a *:* relationship. * Retrieve the query for a *:* relationship.
* *
* @param string $model * @param string $model
* @param string $table
* @param object $eloquent * @param object $eloquent
* @return mixed * @return mixed
*/ */
public static function has_many_and_belongs_to($model, $eloquent) public static function has_many_and_belongs_to($model, $table, $eloquent)
{ {
// ----------------------------------------------------- // -----------------------------------------------------
// Get the models involved in the relationship. // Figure out the intermediate table name.
// ----------------------------------------------------- // -----------------------------------------------------
$models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent))); if (is_null($table))
sort($models); {
$models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent)));
sort($models);
$eloquent->relating_table = implode('_', $models);
}
else
{
$eloquent->relating_table = $table;
}
$eloquent->relating_table = implode('_', $models);
$eloquent->relating = __FUNCTION__; $eloquent->relating = __FUNCTION__;
$eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id'; $eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id';
......
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