From 176f0862aaa4bb2206fa39ae17f271d4140662de Mon Sep 17 00:00:00 2001 From: Drazen Date: Wed, 28 Mar 2012 11:09:47 +0200 Subject: [PATCH 01/12] Updated upgrade guide, Blade is also needed in the aliases Signed-off-by: Drazen --- changes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changes.md b/changes.md index 9e5f3e02..df25ac79 100644 --- a/changes.md +++ b/changes.md @@ -62,6 +62,7 @@ ### Add alias for Eloquent in your application configuration. 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. From 17cabd47e6e9306863e0e9b3567ac6cbaa788761 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 14:12:03 -0500 Subject: [PATCH 02/12] Added to_array() and $hidden variable to the Eloquent base model. Signed-off-by: Taylor Otwell --- laravel/database/eloquent/model.php | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 763cd46f..0bcda60f 100644 --- a/laravel/database/eloquent/model.php +++ b/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 @@ final public function purge($key) 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. * From 1b67691e08b99d136efab856d32bcfdffa758b4c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 14:13:27 -0500 Subject: [PATCH 03/12] Updated change log. Signed-off-by: Taylor Otwell --- changes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changes.md b/changes.md index f15838f9..3de29ddb 100644 --- a/changes.md +++ b/changes.md @@ -13,6 +13,8 @@ ## Contents ## Laravel 3.2 - 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. ## Upgrading From 3.1 From b2324a757541d11755d74278a52e0d5f53dc58f9 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 14:21:19 -0500 Subject: [PATCH 04/12] upgrade change log for redis database option. --- changes.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/changes.md b/changes.md index 4f638ad5..d8ca833c 100644 --- a/changes.md +++ b/changes.md @@ -89,4 +89,16 @@ ### Update your **config/strings.php** file. ### 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. \ No newline at end of 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 + ), + ), \ No newline at end of file From c6125edc6625a423c1fd7729840a4dcdc3f02cd6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 15:14:46 -0500 Subject: [PATCH 05/12] fix bug in input::except method when passing a string. --- changes.md | 1 + laravel/input.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/changes.md b/changes.md index 62c81cc0..902abaac 100644 --- a/changes.md +++ b/changes.md @@ -12,6 +12,7 @@ ## Contents ## 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. diff --git a/laravel/input.php b/laravel/input.php index 8524957e..4f8afa45 100644 --- a/laravel/input.php +++ b/laravel/input.php @@ -96,7 +96,7 @@ public static function only($keys) */ public static function except($keys) { - return array_diff_key(static::get(), array_flip($keys)); + return array_diff_key(static::get(), array_flip((array) $keys)); } /** From cd71c1e517085a4cb50789d69b392d8c8387745c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 16:20:38 -0500 Subject: [PATCH 06/12] added sync method to has many and belongs to entity relationship. --- .../relationships/has_many_and_belongs_to.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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 225f7eb8..9667c099 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -86,6 +86,51 @@ public function attach($id, $attributes = array()) 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. * From 7c9228b40bb08272ae8ccb0fc880b8fc1b091ef5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 16:23:15 -0500 Subject: [PATCH 07/12] update change log. --- changes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changes.md b/changes.md index 902abaac..8058e378 100644 --- a/changes.md +++ b/changes.md @@ -16,6 +16,7 @@ ## Laravel 3.2 - 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. ## Upgrading From 3.1 From 144117c0a157a0df422cc3f91f3aa4fc7cb062f3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 16:58:08 -0500 Subject: [PATCH 08/12] cleaning up the eloquent model class. --- laravel/database/eloquent/model.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 0bcda60f..efc493b2 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -662,10 +662,12 @@ public function __unset($key) */ 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; } From 579bc8719f5dce962bb32cd91931799aba50fb1d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 17:13:13 -0500 Subject: [PATCH 09/12] cleaning up code. --- laravel/database/eloquent/query.php | 2 +- .../relationships/has_many_and_belongs_to.php | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/laravel/database/eloquent/query.php b/laravel/database/eloquent/query.php index c4367e96..87d9b05e 100644 --- a/laravel/database/eloquent/query.php +++ b/laravel/database/eloquent/query.php @@ -183,7 +183,7 @@ protected function load(&$results, $relationship, $constraints) $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); 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 9667c099..e030ea72 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -239,7 +239,7 @@ protected function set_select($foreign, $other) $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) { @@ -314,9 +314,14 @@ public function match($relationship, &$parents, $children) { $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; } } @@ -337,7 +342,7 @@ protected function hydrate_pivot(&$results) // 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_')) @@ -367,9 +372,9 @@ public function with($columns) { $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()); From b5dd77d387c64ab65a732c2b7b838fb3956fd869 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 22:04:04 -0500 Subject: [PATCH 10/12] Fixing Eloquent constructor bug. Signed-off-by: Taylor Otwell --- changes.md | 12 ++++++++++++ laravel/database/eloquent/query.php | 4 ++-- .../database/eloquent/relationships/relationship.php | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/changes.md b/changes.md index ec6ff4bd..761388b9 100644 --- a/changes.md +++ b/changes.md @@ -2,11 +2,23 @@ ## Laravel Change Log ## Contents +- [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) - [Upgrading From 3.0](#upgrade-3.1) + +## Laravel 3.1.2 + +- Fixes Eloquent query method constructor conflict. + + +## Upgrade From 3.1.1 + +- Replace the **laravel** folder. + ## Laravel 3.1.1 diff --git a/laravel/database/eloquent/query.php b/laravel/database/eloquent/query.php index c4367e96..e30fe0e2 100644 --- a/laravel/database/eloquent/query.php +++ b/laravel/database/eloquent/query.php @@ -43,7 +43,7 @@ public function __construct($model) { $this->model = ($model instanceof Model) ? $model : new $model; - $this->table = $this->query(); + $this->table = $this->table(); } /** @@ -245,7 +245,7 @@ protected function model_includes() * * @return Query */ - protected function query() + protected function table() { return $this->connection()->table($this->model->table()); } diff --git a/laravel/database/eloquent/relationships/relationship.php b/laravel/database/eloquent/relationships/relationship.php index 84d02362..2ec10aa4 100644 --- a/laravel/database/eloquent/relationships/relationship.php +++ b/laravel/database/eloquent/relationships/relationship.php @@ -51,7 +51,7 @@ public function __construct($model, $associated, $foreign) // 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(); } From 67758b693265386b3f80e7c378bd571c38ff2f21 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 22:06:20 -0500 Subject: [PATCH 11/12] update change log. --- artisan | 2 +- paths.php | 2 +- public/index.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/artisan b/artisan index d4261a1d..68e645ea 100644 --- a/artisan +++ b/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 * @link http://laravel.com */ diff --git a/paths.php b/paths.php index 7558c326..7d9e1906 100644 --- a/paths.php +++ b/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 * @link http://laravel.com */ diff --git a/public/index.php b/public/index.php index 20f48395..f76527dd 100644 --- a/public/index.php +++ b/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 * @link http://laravel.com */ From b870ac09dd1ef2b4b4e86acd71f02c9b38ad0060 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Mar 2012 22:06:39 -0500 Subject: [PATCH 12/12] fix front controller. --- public/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index f76527dd..42b6a438 100644 --- a/public/index.php +++ b/public/index.php @@ -31,4 +31,4 @@ // -------------------------------------------------------------- // Launch Laravel. // -------------------------------------------------------------- -require path('sys').'laravel.php'; +require path('sys').'laravel.php'; \ No newline at end of file