Commit dc92dd26 authored by Taylor Otwell's avatar Taylor Otwell

Adding auto detection of intermediate table names.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent c3d95122
......@@ -264,7 +264,7 @@ abstract class Model {
* @param string $other
* @return Relationship
*/
public function has_many_and_belongs_to($model, $table, $foreign = null, $other = null)
public function has_many_and_belongs_to($model, $table = null, $foreign = null, $other = null)
{
return new Has_Many_And_Belongs_To($this, $model, $table, $foreign, $other);
}
......
......@@ -39,11 +39,27 @@ class Has_Many_And_Belongs_To extends Relationship {
{
$this->other = $other;
$this->joining = $table;
$this->joining = $table ?: $this->joining($model, $associated);
parent::__construct($model, $associated, $foreign);
}
/**
* Determine the joining table name for the relationship.
*
* By default, the name is the models sorted and concatenated with an underscore.
*
* @return string
*/
protected function joining($model, $associated)
{
$models = array(class_basename($model), class_basename($associated));
sort($models);
return strtolower($models[0].'_'.$models[1]);
}
/**
* Get the properly hydrated results for the relationship.
*
......
......@@ -376,6 +376,21 @@ function root_namespace($class, $separator = '\\')
}
}
/**
* Get the "class basename" of a class or object.
*
* The basename is considered the name of the class minus all namespaces.
*
* @param object|string $class
* @return string
*/
function class_basename($class)
{
if (is_object($class)) $class = get_class($class);
return basename($class);
}
/**
* Return the value of the given item.
*
......
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