Skip to content

Commit

Permalink
made relation names in AR case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Nov 7, 2013
1 parent 90839ce commit a23c54a
Showing 1 changed file with 7 additions and 28 deletions.
35 changes: 7 additions & 28 deletions framework/yii/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public function hasMany($class, $link)
*/
public function populateRelation($name, $records)
{
$this->_related[strtolower($name)] = $records;
$this->_related[$name] = $records;
}

/**
Expand All @@ -538,36 +538,15 @@ public function populateRelation($name, $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_key_exists($name, $this->_related);
}

/**
* @return array all populated relations
*/
public function getPopulatedRelations()
{
$relations = $this->getPopulatedRelationNames();
$data = [];
foreach($relations as $name) {
$data[$name] = $this->_related[strtolower($name)];
}
return $data;
return $this->_related;
}

/**
Expand Down Expand Up @@ -1325,7 +1304,7 @@ public function getRelation($name)
*
* 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
Expand All @@ -1347,7 +1326,7 @@ public function link($name, $model, $extraColumns = [])
$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);
Expand Down Expand Up @@ -1403,7 +1382,7 @@ public function link($name, $model, $extraColumns = [])
* 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.
Expand All @@ -1421,7 +1400,7 @@ public function unlink($name, $model, $delete = false)
/** @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);
Expand Down

0 comments on commit a23c54a

Please sign in to comment.