From 98a691fa58640df321f12b8bf67a705f2d474312 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 08:28:42 -0700 Subject: [PATCH] Remove comment bloat from Eloquent. --- system/db/eloquent.php | 99 ++++++++++-------------------------------- 1 file changed, 24 insertions(+), 75 deletions(-) diff --git a/system/db/eloquent.php b/system/db/eloquent.php index 9263faab..28f23c86 100644 --- a/system/db/eloquent.php +++ b/system/db/eloquent.php @@ -116,11 +116,8 @@ public static function make($class) { $model = new $class; - // ----------------------------------------------------- - // Since this method is only used for instantiating - // models for querying purposes, we will go ahead and - // set the Query instance on the model. - // ----------------------------------------------------- + // Since this method is only used for instantiating/ models for querying + // purposes, we will go ahead and set the Query instance on the model. $model->query = Query::table(static::table($class)); return $model; @@ -206,14 +203,9 @@ public function has_many($model, $foreign_key = null) */ private function has_one_or_many($model, $foreign_key) { - // ----------------------------------------------------- - // The default foreign key for has one and has many - // relationships is the name of the model with an - // appended _id. - // - // For example, the foreign key for a User model would - // be user_id. Photo would be photo_id, etc. - // ----------------------------------------------------- + // The default foreign key for has one and has many relationships is the name + // of the model with an appended _id. For example, the foreign key for a + // User model would be user_id. Photo would be photo_id, etc. $this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key; return static::make($model)->where($this->relating_key, '=', $this->id); @@ -236,13 +228,9 @@ public function belongs_to($model, $foreign_key = null) } else { - // ----------------------------------------------------- - // The default foreign key for belonging relationships - // is the name of the relationship method name with _id. - // - // So, if a model has a "manager" method returning a - // belongs_to relationship, the key would be manager_id. - // ----------------------------------------------------- + // The default foreign key for belonging relationships is the name of the + // relationship method name with _id. So, if a model has a "manager" method + // returning a belongs_to relationship, the key would be manager_id. list(, $caller) = debug_backtrace(false); $this->relating_key = $caller['function'].'_id'; @@ -268,11 +256,8 @@ public function has_and_belongs_to_many($model, $table = null) } else { - // ----------------------------------------------------- - // By default, the intermediate table name is the plural - // names of the models arranged alphabetically and - // concatenated with an underscore. - // ----------------------------------------------------- + // By default, the intermediate table name is the plural names of the models + // arranged alphabetically and concatenated with an underscore. $models = array(Inflector::plural($model), Inflector::plural(get_class($this))); sort($models); @@ -280,13 +265,8 @@ public function has_and_belongs_to_many($model, $table = null) $this->relating_table = strtolower($models[0].'_'.$models[1]); } - // ----------------------------------------------------- - // The default foreign key for many-to-many relations - // is the name of the model with an appended _id. - // appended _id. - // + // The default foreign key for many-to-many relations is the name of the model with an appended _id. // This is the same convention as has_one and has_many. - // ----------------------------------------------------- $this->relating_key = strtolower(get_class($this)).'_id'; return static::make($model) @@ -302,10 +282,6 @@ public function has_and_belongs_to_many($model, $table = null) */ 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; @@ -313,15 +289,10 @@ public function save() $model = get_class($this); - // ----------------------------------------------------- - // Since the model was instantiated using "new", a query - // instance has not been set. We'll do it now. - // ----------------------------------------------------- + // Since the model was instantiated using "new", a query instance has not been set. $this->query = Query::table(static::table($model)); - // ----------------------------------------------------- // Set the creation and update timestamps. - // ----------------------------------------------------- if (property_exists($model, 'timestamps') and $model::$timestamps) { $this->updated_at = date('Y-m-d H:i:s'); @@ -332,10 +303,8 @@ public function save() } } - // ----------------------------------------------------- - // If the model already exists in the database, we only - // need to update it. Otherwise, we'll insert it. - // ----------------------------------------------------- + // If the model already exists in the database, we only need to update it. + // Otherwise, we'll insert the model into the database. if ($this->exists) { $result = $this->query->where('id', '=', $this->attributes['id'])->update($this->dirty) == 1; @@ -360,16 +329,12 @@ public function save() */ 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 Query::table(static::table(get_class($this)))->delete($this->id); } - return $this->query->delete($id); + return 0; } /** @@ -377,18 +342,15 @@ public function delete($id = null) */ public function __get($key) { - // ----------------------------------------------------- - // Check the ignored attributes first. These attributes - // hold all of the loaded relationships. - // ----------------------------------------------------- + // Check the ignored attributes first. These attributes hold all of the + // loaded relationships for the model. if (array_key_exists($key, $this->ignore)) { return $this->ignore[$key]; } - // ----------------------------------------------------- - // Is the attribute actually a relationship method? - // ----------------------------------------------------- + // Is the attribute actually a relationship method? If it is, return the + // models for the relationship. if (method_exists($this, $key)) { $model = $this->$key(); @@ -406,10 +368,7 @@ public function __get($key) */ public function __set($key, $value) { - // ----------------------------------------------------- - // If the key is a relationship, add it to the ignored. - // Otherwise, we can simply add it as an attribute. - // ----------------------------------------------------- + // If the key is a relationship, add it to the ignored attributes. if (method_exists($this, $key)) { $this->ignore[$key] = $value; @@ -454,18 +413,13 @@ public function __call($method, $parameters) return $this->_first(); } - // ----------------------------------------------------- - // Pass aggregate methods to the query instance. - // ----------------------------------------------------- if (in_array($method, array('count', 'sum', 'min', 'max', 'avg'))) { return call_user_func_array(array($this->query, $method), $parameters); } - // ----------------------------------------------------- - // Pass the method to the query instance. This allows - // the chaining of methods from the query builder. - // ----------------------------------------------------- + // Pass the method to the query instance. This allows the chaining of methods + // from the query builder, providing a nice, convenient API. call_user_func_array(array($this->query, $method), $parameters); return $this; @@ -488,18 +442,13 @@ public static function __callStatic($method, $parameters) return $model->_first(); } - // ----------------------------------------------------- - // Pass aggregate methods to the query instance. - // ----------------------------------------------------- if (in_array($method, array('count', 'sum', 'min', 'max', 'avg'))) { return call_user_func_array(array($model->query, $method), $parameters); } - // ----------------------------------------------------- - // Pass the method to the query instance. This allows - // the chaining of methods from the query builder. - // ----------------------------------------------------- + // Pass the method to the query instance. This allows the chaining of methods + // from the query builder, providing a nice, convenient API. call_user_func_array(array($model->query, $method), $parameters); return $model;