Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for accessing populated relations #1125

Merged
merged 4 commits into from Nov 7, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 30 additions & 13 deletions framework/yii/db/ActiveRecord.php
Expand Up @@ -379,13 +379,12 @@ public function __get($name)
} 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;
}
Expand Down Expand Up @@ -433,9 +432,8 @@ public function __unset($name)
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);
}
Expand Down Expand Up @@ -523,12 +521,31 @@ public function hasMany($class, $link)
/**
* 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)
{
$this->_related[strtolower($name)] = $records;
$this->_related[$name] = $records;
}

/**
* Check whether the named relation has been populated with records.
* @param string $name the relation name (case-sensitive)
* @return bool whether relation has been populated with records.
*/
public function isRelationPopulated($name)
{
return array_key_exists($name, $this->_related);
}

/**
* Returns all populated relations.
* @return array an array of relation data indexed by relation names.
*/
public function getPopulatedRelations()
{
return $this->_related;
}

/**
Expand Down Expand Up @@ -1286,7 +1303,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 @@ -1308,7 +1325,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 @@ -1364,7 +1381,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 @@ -1382,7 +1399,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