From c30185eb27c81e01f2087bcdbe566d2f6e968c01 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 10 Jul 2011 23:31:37 -0500 Subject: [PATCH] refactoring eloquent. --- system/db/eloquent.php | 49 +++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/system/db/eloquent.php b/system/db/eloquent.php index a705cc5b..f63d265c 100644 --- a/system/db/eloquent.php +++ b/system/db/eloquent.php @@ -274,19 +274,16 @@ public function has_and_belongs_to_many($model, $table = null) $this->relating_key = strtolower(get_class($this)).'_id'; - $relationship = static::make($model); - - $relationship->select(array(static::table($model).'.*')); - $relationship->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.strtolower($model).'_id'); - $relationship->where($this->relating_table.'.'.$this->relating_key, '=', $this->id); - - return $relationship; + return static::make($model) + ->select(array(static::table($model).'.*')) + ->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.strtolower($model).'_id') + ->where($this->relating_table.'.'.$this->relating_key, '=', $this->id); } /** * Save the model to the database. * - * @return bool + * @return void */ public function save() { @@ -303,23 +300,28 @@ public function save() if (property_exists($model, 'timestamps') and $model::$timestamps) { - $this->timestamp(); + $this->updated_at = date('Y-m-d H:i:s'); + + if ( ! $this->exists) + { + $this->created_at = $this->updated_at; + } } + // If the model already exists in the database, we will just update it. + // Otherwise, we will insert the model and set the ID attribute. if ($this->exists) { - $result = $this->query->where('id', '=', $this->attributes['id'])->update($this->dirty) == 1; + $this->query->where('id', '=', $this->attributes['id'])->update($this->dirty); } else { $this->attributes['id'] = $this->query->insert_get_id($this->attributes); - - $result = $this->exists = is_numeric($this->id); } - $this->dirty = array(); + $this->exists = true; - return $result; + $this->dirty = array(); } /** @@ -338,21 +340,6 @@ public function delete($id = null) return $this->query->delete(); } - /** - * Set the creation and update timestamps on the model. - * - * @return void - */ - private function timestamp() - { - $this->updated_at = date('Y-m-d H:i:s'); - - if ( ! $this->exists) - { - $this->created_at = $this->updated_at; - } - } - /** * Magic method for retrieving model attributes. */ @@ -405,9 +392,7 @@ public function __isset($key) */ public function __unset($key) { - unset($this->attributes[$key]); - unset($this->ignore[$key]); - unset($this->dirty[$key]); + unset($this->attributes[$key], $this->ignore[$key], $this->dirty[$key]); } /**