Fixing bugs and improving.

Signed-off-by: Taylor Otwell <taylorotwell@gmail.com>
This commit is contained in:
Taylor Otwell 2012-03-16 15:32:26 -05:00
parent b5e75f6fcf
commit 762f2402c3
4 changed files with 26 additions and 6 deletions

View File

@ -59,7 +59,7 @@ abstract class Model {
* *
* @var bool * @var bool
*/ */
public static $timestamps = false; public static $timestamps = true;
/** /**
* The name of the table associated with the model. * The name of the table associated with the model.

View File

@ -1,5 +1,6 @@
<?php namespace Laravel\Database\Eloquent\Relationships; <?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Database\Eloquent\Model;
use Laravel\Database\Eloquent\Pivot; use Laravel\Database\Eloquent\Pivot;
class Has_Many_And_Belongs_To extends Relationship { class Has_Many_And_Belongs_To extends Relationship {
@ -23,7 +24,7 @@ class Has_Many_And_Belongs_To extends Relationship {
* *
* @var array * @var array
*/ */
protected $with = array('created_at', 'updated_at'); protected $with = array('id', 'created_at', 'updated_at');
/** /**
* Create a new many to many relationship instance. * Create a new many to many relationship instance.
@ -93,11 +94,17 @@ public function add($id, $attributes = array())
*/ */
public function insert($attributes, $joining = array()) public function insert($attributes, $joining = array())
{ {
$id = $this->table->insert_get_id($attributes, $this->model->sequence()); $model = $this->model->create($attributes);
$result = $this->insert_joining(array_merge($this->join_record($id), $joining)); // If the insert was successful, we'll insert a record into the joining table
// using the new ID that was just inserted into the related table, allowing
// the developer to not worry about maintaining the join table.
if ($model instanceof Model and is_numeric($id = $model->get_key()))
{
$result = $this->insert_joining(array_merge($this->join_record($id), $joining));
}
return is_numeric($id) and $result; return $model instanceof Model and $result;
} }
/** /**

View File

@ -14,7 +14,7 @@ public function insert($attributes)
{ {
$attributes[$this->foreign_key()] = $this->base->get_key(); $attributes[$this->foreign_key()] = $this->base->get_key();
return parent::insert($attributes); return $this->model->create($attributes);
} }
/** /**

View File

@ -68,6 +68,19 @@ public static function foreign($model, $foreign = null)
return strtolower(basename($model).'_id'); return strtolower(basename($model).'_id');
} }
/**
* Get a freshly instantiated instance of the related model class.
*
* @param array $attributes
* @return Model
*/
protected function fresh_model($attributes = array())
{
$class = get_class($this->model);
return new $class($attributes);
}
/** /**
* Get the foreign key for the relationship. * Get the foreign key for the relationship.
* *