Browse Source

Merge branch 'develop' into feature/http-foundation

Taylor Otwell 13 years ago
parent
commit
6234905e4c

+ 1 - 1
artisan

@@ -3,7 +3,7 @@
  * Laravel - A PHP Framework For Web Artisans
  *
  * @package  Laravel
- * @version  3.1.1
+ * @version  3.1.2
  * @author   Taylor Otwell <taylorotwell@gmail.com>
  * @link     http://laravel.com
  */

+ 31 - 1
changes.md

@@ -4,6 +4,8 @@
 
 - [Laravel 3.2](#3.2)
 - [Upgrading From 3.1](#upgrade-3.2)
+- [Laravel 3.1.2](#3.1.2)
+- [Upgrading From 3.1.1](#upgrade-3.1.2)
 - [Laravel 3.1.1](#3.1.1)
 - [Upgrading From 3.1](#upgrade-3.1.1)
 - [Laravel 3.1](#3.1)
@@ -12,11 +14,26 @@
 <a name="3.2"></a>
 ## Laravel 3.2
 
+- Fixed the passing of strings into the Input::except method.
 - Fixed replacement of optional parameters in URL::transpose method.
+- Added "to_array" method to the base Eloquent model.
+- Added "$hidden" static variable to the base Eloquent model.
+- Added "sync" method to has_many_and_belongs_to Eloquent relationship.
 
 <a name="upgrade-3.2"></a>
 ## Upgrading From 3.1
 
+- Replace the **laravel** folder.
+- Add new **vendors** folder.
+
+<a name="3.1.2"></a>
+## Laravel 3.1.2
+
+- Fixes Eloquent query method constructor conflict.
+
+<a name="upgrade-3.1.2"></a>
+## Upgrade From 3.1.1
+
 - Replace the **laravel** folder.
 
 <a name="3.1.1"></a>
@@ -86,6 +103,7 @@ If you have created indexes on tables using the Laravel migration system and you
 Add the following to the **aliases** array in your **application/config/application.php** file:
 
 	'Eloquent' => 'Laravel\\Database\\Eloquent\\Model',
+	'Blade' => 'Laravel\\Blade',
 
 ### Update Eloquent many-to-many tables.
 
@@ -101,4 +119,16 @@ English pluralization and singularization is now automatic. Just completely repl
 
 ### Add the **fetch** option to your database configuration file.
 
-A new **fetch** option allows you to specify in which format you receive your database results. Just copy and paste the option from the new **application/config/database.php** file.
+A new **fetch** option allows you to specify in which format you receive your database results. Just copy and paste the option from the new **application/config/database.php** file.
+
+### Add **database** option to your Redis configuration.
+
+If you are using Redis, add the "database" option to your Redis connection configurations. The "database" value can be zero by default.
+
+	'redis' => array(
+		'default' => array(
+			'host' => '127.0.0.1',
+			'port' => 6379,
+			'database' => 0
+		),
+	),

+ 55 - 1
laravel/database/eloquent/model.php

@@ -55,6 +55,13 @@ abstract class Model {
 	 */
 	public static $accessible;
 
+	/**
+	 * The attributes that should be excluded from to_array.
+	 *
+	 * @var array
+	 */
+	public static $hidden = array();
+
 	/**
 	 * Indicates if the model has update and creation timestamps.
 	 *
@@ -520,6 +527,51 @@ abstract class Model {
 		unset($this->attributes[$key]);
 	}
 
+	/**
+	 * Get the model attributes and relationships in array form.
+	 *
+	 * @return array
+	 */
+	public function to_array()
+	{
+		$attributes = array();
+
+		// First we need to gather all of the regular attributes. If the attribute
+		// exists in the array of "hidden" attributes, it will not be added to
+		// the array so we can easily exclude things like passwords, etc.
+		foreach (array_keys($this->attributes) as $attribute)
+		{
+			if ( ! in_array($attribute, static::$hidden))
+			{
+				$attributes[$attribute] = $this->$attribute;
+			}
+		}
+
+		foreach ($this->relationships as $name => $models)
+		{
+			// If the relationship is not a "to-many" relationship, we can just
+			// to_array the related model and add it as an attribute to the
+			// array of existing regular attributes we gathered.
+			if ( ! is_array($models))
+			{
+				$attributes[$name] = $models->to_array();
+			}
+
+			// If the relationship is a "to-many" relationship we need to spin
+			// through each of the related models and add each one with the
+			// to_array method, keying them both by name and ID.
+			else
+			{
+				foreach ($models as $id => $model)
+				{
+					$attributes[$name][$id] = $model->to_array();
+				}
+			}
+		}
+
+		return $attributes;
+	}
+
 	/**
 	 * Handle the dynamic retrieval of attributes and associations.
 	 *
@@ -610,10 +662,12 @@ abstract class Model {
 	 */
 	public function __call($method, $parameters)
 	{
+		$meta = array('key', 'table', 'connection', 'sequence', 'per_page');
+
 		// If the method is actually the name of a static property on the model we'll
 		// return the value of the static property. This makes it convenient for
 		// relationships to access these values off of the instances.
-		if (in_array($method, array('key', 'table', 'connection', 'sequence', 'per_page')))
+		if (in_array($method, $meta))
 		{
 			return static::$$method;
 		}

+ 3 - 3
laravel/database/eloquent/query.php

@@ -43,7 +43,7 @@ class Query {
 	{
 		$this->model = ($model instanceof Model) ? $model : new $model;
 
-		$this->table = $this->query();
+		$this->table = $this->table();
 	}
 
 	/**
@@ -183,7 +183,7 @@ class Query {
 			$query->table->where_nested($constraints);
 		}
 
-		// Before matching the models, we will initialize the relationship
+		// Before matching the models, we will initialize the relationships
 		// to either null for single-value relationships or an array for
 		// the multi-value relationships as their baseline value.
 		$query->initialize($results, $relationship);
@@ -245,7 +245,7 @@ class Query {
 	 *
 	 * @return Query
 	 */
-	protected function query()
+	protected function table()
 	{
 		return $this->connection()->table($this->model->table());
 	}

+ 56 - 6
laravel/database/eloquent/relationships/has_many_and_belongs_to.php

@@ -86,6 +86,51 @@ class Has_Many_And_Belongs_To extends Relationship {
 		return $this->insert_joining($joining);
 	}
 
+	/**
+	 * Detach a record from the joining table of the association.
+	 *
+	 * @param  int   $ids
+	 * @return bool
+	 */
+	public function detach($ids)
+	{
+		if ( ! is_array($ids)) $ids = array($ids);
+
+		return $this->pivot()->where_in($this->other_key(), $ids)->delete();
+	}
+
+	/**
+	 * Sync the joining table with the array of given IDs.
+	 *
+	 * @param  array  $ids
+	 * @return bool
+	 */
+	public function sync($ids)
+	{
+		$current = $this->pivot()->lists($this->other_key());
+
+		// First we need to attach any of the associated models that are not currently
+		// in the joining table. We'll spin through the given IDs, checking to see
+		// if they exist in the array of current ones, and if not we insert.
+		foreach ($ids as $id)
+		{
+			if ( ! in_array($id, $current))
+			{
+				$this->attach($id);
+			}
+		}
+
+		// Next we will take the difference of the current and given IDs and detach
+		// all of the entities that exists in the current array but are not in
+		// the array of IDs given to the method, finishing the sync.
+		$detach = array_diff($current, $ids);
+
+		if (count($detach) > 0)
+		{
+			$this->detach(array_diff($current, $ids));
+		}
+	}
+
 	/**
 	 * Insert a new record for the association.
 	 *
@@ -194,7 +239,7 @@ class Has_Many_And_Belongs_To extends Relationship {
 		$this->with = array_merge($this->with, array($foreign, $other));
 
 		// Since pivot tables may have extra information on them that the developer
-		// needs, we allow an extra array of columns to be specified that will be
+		// needs we allow an extra array of columns to be specified that will be
 		// fetched from the pivot table and hydrate into the pivot model.
 		foreach ($this->with as $column)
 		{
@@ -269,9 +314,14 @@ class Has_Many_And_Belongs_To extends Relationship {
 	{
 		$foreign = $this->foreign_key();
 
+		// For each child we'll just get the parent that connects to the child and set the
+		// child model on the relationship array using the keys. Once we're done looping
+		// through the children all of the proper relations will be set.
 		foreach ($children as $key => $child)
 		{
-			$parents[$child->pivot->$foreign]->relationships[$relationship][$child->{$child->key()}] = $child;
+			$parent =& $parents[$child->pivot->$foreign];
+
+			$parent->relationships[$relationship][$child->{$child->key()}] = $child;
 		}
 	}
 
@@ -292,7 +342,7 @@ class Has_Many_And_Belongs_To extends Relationship {
 
 			// If the attribute key starts with "pivot_", we know this is a column on
 			// the pivot table, so we will move it to the Pivot model and purge it
-			// from the model since it actually belongs to the pivot.
+			// from the model since it actually belongs to the pivot model.
 			foreach ($result->attributes as $key => $value)
 			{
 				if (starts_with($key, 'pivot_'))
@@ -322,9 +372,9 @@ class Has_Many_And_Belongs_To extends Relationship {
 	{
 		$columns = (is_array($columns)) ? $columns : func_get_args();
 
-		// The "with" array contains a couple of columns by default, so we will
-		// just merge in the developer specified columns here, and we'll make
-		// sure the values of the array are unique.
+		// The "with" array contains a couple of columns by default, so we will just
+		// merge in the developer specified columns here, and we will make sure
+		// the values of the array are unique to avoid duplicates.
 		$this->with = array_unique(array_merge($this->with, $columns));
 
 		$this->set_select($this->foreign_key(), $this->other_key());

+ 1 - 1
laravel/database/eloquent/relationships/relationship.php

@@ -51,7 +51,7 @@ abstract class Relationship extends Query {
 		// Next we'll set the fluent query builder for the relationship and
 		// constrain the query such that it only returns the models that
 		// are appropriate for the relationship.
-		$this->table = $this->query();
+		$this->table = $this->table();
 
 		$this->constrain();
 	}

+ 1 - 1
laravel/input.php

@@ -120,7 +120,7 @@ class Input {
 	 */
 	public static function except($keys)
 	{
-		return array_diff_key(static::get(), array_flip($keys));
+		return array_diff_key(static::get(), array_flip((array) $keys));
 	}
 
 	/**

+ 1 - 1
paths.php

@@ -3,7 +3,7 @@
  * Laravel - A PHP Framework For Web Artisans
  *
  * @package  Laravel
- * @version  3.1.1
+ * @version  3.1.2
  * @author   Taylor Otwell <taylorotwell@gmail.com>
  * @link     http://laravel.com
  */

+ 2 - 2
public/index.php

@@ -3,7 +3,7 @@
  * Laravel - A PHP Framework For Web Artisans
  *
  * @package  Laravel
- * @version  3.1.1
+ * @version  3.1.2
  * @author   Taylor Otwell <taylorotwell@gmail.com>
  * @link     http://laravel.com
  */
@@ -31,4 +31,4 @@ unset($web);
 // --------------------------------------------------------------
 // Launch Laravel.
 // --------------------------------------------------------------
-require path('sys').'laravel.php';
+require path('sys').'laravel.php';