From 762f2402c3fb171e34ef76966c7e5b6d49525538 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 16 Mar 2012 15:32:26 -0500 Subject: [PATCH] Fixing bugs and improving. Signed-off-by: Taylor Otwell --- laravel/database/eloquent/model.php | 2 +- .../relationships/has_many_and_belongs_to.php | 15 +++++++++++---- .../eloquent/relationships/has_one_or_many.php | 2 +- .../eloquent/relationships/relationship.php | 13 +++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index bde5ac46..0b624051 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -59,7 +59,7 @@ abstract class Model { * * @var bool */ - public static $timestamps = false; + public static $timestamps = true; /** * The name of the table associated with the model. diff --git a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php index 8c8abee1..95989e9e 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -1,5 +1,6 @@ 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; } /** diff --git a/laravel/database/eloquent/relationships/has_one_or_many.php b/laravel/database/eloquent/relationships/has_one_or_many.php index 211eb7db..3c9ad4a6 100644 --- a/laravel/database/eloquent/relationships/has_one_or_many.php +++ b/laravel/database/eloquent/relationships/has_one_or_many.php @@ -14,7 +14,7 @@ public function insert($attributes) { $attributes[$this->foreign_key()] = $this->base->get_key(); - return parent::insert($attributes); + return $this->model->create($attributes); } /** diff --git a/laravel/database/eloquent/relationships/relationship.php b/laravel/database/eloquent/relationships/relationship.php index aabfe427..8d1ebf1b 100644 --- a/laravel/database/eloquent/relationships/relationship.php +++ b/laravel/database/eloquent/relationships/relationship.php @@ -68,6 +68,19 @@ public static function foreign($model, $foreign = null) 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. *