Automatically detect eloquent table names.

This commit is contained in:
Taylor Otwell 2012-03-19 14:11:11 -05:00
parent 26afb000bf
commit e540fd3b6d
2 changed files with 14 additions and 2 deletions

View File

@ -1,5 +1,6 @@
<?php namespace Laravel\Database\Eloquent; <?php namespace Laravel\Database\Eloquent;
use Laravel\Str;
use Laravel\Database; use Laravel\Database;
use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To; use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
@ -418,6 +419,16 @@ public function dirty()
return ! $this->exists or $this->original !== $this->attributes; return ! $this->exists or $this->original !== $this->attributes;
} }
/**
* Get the name of the table associated with the model.
*
* @return string
*/
public function table()
{
return static::$table ?: strtolower(Str::plural(basename(get_class($this))));
}
/** /**
* Get the dirty attributes for the model. * Get the dirty attributes for the model.
* *

View File

@ -1,5 +1,6 @@
<?php namespace Laravel\Database\Eloquent\Relationships; <?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Str;
use Laravel\Database\Eloquent\Model; use Laravel\Database\Eloquent\Model;
use Laravel\Database\Eloquent\Pivot; use Laravel\Database\Eloquent\Pivot;
@ -48,7 +49,7 @@ public function __construct($model, $associated, $table, $foreign, $other)
/** /**
* Determine the joining table name for the relationship. * Determine the joining table name for the relationship.
* *
* By default, the name is the models sorted and concatenated with an underscore. * By default, the name is the models sorted and joined with underscores.
* *
* @return string * @return string
*/ */
@ -140,7 +141,7 @@ protected function insert_joining($attributes)
{ {
// All joining tables get creation and update timestamps automatically even though // All joining tables get creation and update timestamps automatically even though
// some developers may not need them. This just provides them if necessary since // some developers may not need them. This just provides them if necessary since
// it would be a pain for the developer to maintain them manually. // it would be a pain for the developer to maintain them each manually.
$attributes['created_at'] = $this->model->get_timestamp(); $attributes['created_at'] = $this->model->get_timestamp();
$attributes['updated_at'] = $attributes['created_at']; $attributes['updated_at'] = $attributes['created_at'];