From 90839ceb5d5eb1796cff125011af16ee507f60c7 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 3 Nov 2013 02:08:26 +0100 Subject: [PATCH 1/4] Proposal for accessing populated relations fixes #842 - allows checking whether a relation has been populated - getting a list of relation names that have been populated - getting all populated relation data todo: - [] add phpdoc --- framework/yii/db/ActiveRecord.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 154d7ac..cb99b9f 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -532,6 +532,45 @@ class ActiveRecord extends Model } /** + * Check whether the named relation has been populated with records. + * @param string $name the relation name (case-insensitive) + * @return bool whether relation has been populated with records. + */ + public function isRelationPopulated($name) + { + return array_key_exists(strtolower($name), $this->_related); + } + + /** + * @return array list of populated relation names + */ + public function getPopulatedRelationNames() + { + $relations = array_keys($this->_related); + $reflection = new \ReflectionClass($this); + foreach($relations as $i => $relation) { + if ($reflection->hasMethod('get' . $relation)) { + $method = $reflection->getMethod('get' . $relation); + $relations[$i] = lcfirst(substr($method->name, 3)); + } + } + return $relations; + } + + /** + * @return array all populated relations + */ + public function getPopulatedRelations() + { + $relations = $this->getPopulatedRelationNames(); + $data = []; + foreach($relations as $name) { + $data[$name] = $this->_related[strtolower($name)]; + } + return $data; + } + + /** * Returns the list of all attribute names of the model. * The default implementation will return all column names of the table associated with this AR class. * @return array list of attribute names. From a23c54acb843eedc571a3ccb953486415fe7e98a Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 7 Nov 2013 10:39:35 +0100 Subject: [PATCH 2/4] made relation names in AR case sensitive --- framework/yii/db/ActiveRecord.php | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index cb99b9f..9e7a432 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -528,7 +528,7 @@ class ActiveRecord extends Model */ public function populateRelation($name, $records) { - $this->_related[strtolower($name)] = $records; + $this->_related[$name] = $records; } /** @@ -538,23 +538,7 @@ class ActiveRecord extends Model */ public function isRelationPopulated($name) { - return array_key_exists(strtolower($name), $this->_related); - } - - /** - * @return array list of populated relation names - */ - public function getPopulatedRelationNames() - { - $relations = array_keys($this->_related); - $reflection = new \ReflectionClass($this); - foreach($relations as $i => $relation) { - if ($reflection->hasMethod('get' . $relation)) { - $method = $reflection->getMethod('get' . $relation); - $relations[$i] = lcfirst(substr($method->name, 3)); - } - } - return $relations; + return array_key_exists($name, $this->_related); } /** @@ -562,12 +546,7 @@ class ActiveRecord extends Model */ public function getPopulatedRelations() { - $relations = $this->getPopulatedRelationNames(); - $data = []; - foreach($relations as $name) { - $data[$name] = $this->_related[strtolower($name)]; - } - return $data; + return $this->_related; } /** @@ -1325,7 +1304,7 @@ class ActiveRecord extends Model * * Note that this method requires that the primary key value is not null. * - * @param string $name the name of the relationship + * @param string $name the case sensitive name of the relationship * @param ActiveRecord $model the model to be linked with the current one. * @param array $extraColumns additional column values to be saved into the pivot table. * This parameter is only meaningful for a relationship involving a pivot table @@ -1347,7 +1326,7 @@ class ActiveRecord extends Model $viaClass = $viaRelation->modelClass; $viaTable = $viaClass::tableName(); // unset $viaName so that it can be reloaded to reflect the change - unset($this->_related[strtolower($viaName)]); + unset($this->_related[$viaName]); } else { $viaRelation = $relation->via; $viaTable = reset($relation->via->from); @@ -1403,7 +1382,7 @@ class ActiveRecord extends Model * The model with the foreign key of the relationship will be deleted if `$delete` is true. * Otherwise, the foreign key will be set null and the model will be saved without validation. * - * @param string $name the name of the relationship. + * @param string $name the case sensitive name of the relationship. * @param ActiveRecord $model the model to be unlinked from the current one. * @param boolean $delete whether to delete the model that contains the foreign key. * If false, the model's foreign key will be set null and saved. @@ -1421,7 +1400,7 @@ class ActiveRecord extends Model /** @var $viaClass ActiveRecord */ $viaClass = $viaRelation->modelClass; $viaTable = $viaClass::tableName(); - unset($this->_related[strtolower($viaName)]); + unset($this->_related[$viaName]); } else { $viaRelation = $relation->via; $viaTable = reset($relation->via->from); From c4468ac315e000dcc6504e9f10ef0f1f24e5bfff Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 7 Nov 2013 11:09:35 +0100 Subject: [PATCH 3/4] forgot to remove strtolower from __get and __unset --- framework/yii/db/ActiveRecord.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 9e7a432..8b376b4 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -379,13 +379,12 @@ class ActiveRecord extends Model } elseif ($this->hasAttribute($name)) { return null; } else { - $t = strtolower($name); - if (isset($this->_related[$t]) || array_key_exists($t, $this->_related)) { - return $this->_related[$t]; + if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) { + return $this->_related[$name]; } $value = parent::__get($name); if ($value instanceof ActiveRelation) { - return $this->_related[$t] = $value->multiple ? $value->all() : $value->one(); + return $this->_related[$name] = $value->multiple ? $value->all() : $value->one(); } else { return $value; } @@ -433,9 +432,8 @@ class ActiveRecord extends Model if ($this->hasAttribute($name)) { unset($this->_attributes[$name]); } else { - $t = strtolower($name); - if (isset($this->_related[$t])) { - unset($this->_related[$t]); + if (isset($this->_related[$name])) { + unset($this->_related[$name]); } else { parent::__unset($name); } From 0bc8cbf1589f052d9456483e05530b0676291150 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 7 Nov 2013 11:18:16 +0100 Subject: [PATCH 4/4] php-doc [ci skip] --- framework/yii/db/ActiveRecord.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/framework/yii/db/ActiveRecord.php b/framework/yii/db/ActiveRecord.php index 8b376b4..830bf40 100644 --- a/framework/yii/db/ActiveRecord.php +++ b/framework/yii/db/ActiveRecord.php @@ -521,7 +521,7 @@ class ActiveRecord extends Model /** * Populates the named relation with the related records. * Note that this method does not check if the relation exists or not. - * @param string $name the relation name (case-insensitive) + * @param string $name the relation name (case-sensitive) * @param ActiveRecord|array|null the related records to be populated into the relation. */ public function populateRelation($name, $records) @@ -531,7 +531,7 @@ class ActiveRecord extends Model /** * Check whether the named relation has been populated with records. - * @param string $name the relation name (case-insensitive) + * @param string $name the relation name (case-sensitive) * @return bool whether relation has been populated with records. */ public function isRelationPopulated($name) @@ -540,7 +540,8 @@ class ActiveRecord extends Model } /** - * @return array all populated relations + * Returns all populated relations. + * @return array an array of relation data indexed by relation names. */ public function getPopulatedRelations() {