From 285cdcc8f2f93233bc3c33f9b551b4bfc04e7f42 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 13 Jun 2011 21:34:53 -0500 Subject: [PATCH] fixed bug when deleting eloquent models. --- system/db/eloquent.php | 30 +++++++++++++++++++++++++++++- system/db/eloquent/warehouse.php | 13 ++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/system/db/eloquent.php b/system/db/eloquent.php index 4033f300..91510041 100644 --- a/system/db/eloquent.php +++ b/system/db/eloquent.php @@ -178,12 +178,40 @@ public function has_many_and_belongs_to($model) */ public function save() { + // ----------------------------------------------------- + // If the model doesn't have any dirty attributes, there + // is no need to save it to the database. + // ----------------------------------------------------- if ($this->exists and count($this->dirty) == 0) { return true; } - return Eloquent\Warehouse::store($this); + $result = Eloquent\Warehouse::put($this); + + // ----------------------------------------------------- + // The dirty attributes can be cleared after each save. + // ----------------------------------------------------- + $this->dirty = array(); + + return $result; + } + + /** + * Delete a model from the database. + */ + public function delete($id = null) + { + // ----------------------------------------------------- + // If the method is being called from an existing model, + // only delete that model from the database. + // ----------------------------------------------------- + if ($this->exists) + { + return Eloquent\Warehouse::forget($this); + } + + return $this->query->delete($id); } /** diff --git a/system/db/eloquent/warehouse.php b/system/db/eloquent/warehouse.php index 5f5b1a57..587dfcd7 100644 --- a/system/db/eloquent/warehouse.php +++ b/system/db/eloquent/warehouse.php @@ -8,7 +8,7 @@ class Warehouse { * @param object $eloquent * @return bool */ - public static function store($eloquent) + public static function put($eloquent) { $model = get_class($eloquent); @@ -39,6 +39,17 @@ public static function store($eloquent) return true; } + /** + * Delete an Eloquent model from the database. + * + * @param object $eloquent + * @return bool + */ + public static function forget($eloquent) + { + return (\System\DB::table(Meta::table(get_class($eloquent)))->where('id', '=', $eloquent->id)->delete() == 1); + } + /** * Set the activity timestamps on a model. *