Commit 3701cbd5 by Qiang Xue

Merge pull request #1125 from cebe/842-relation-data

Proposal for accessing populated relations
parents 65d7ffa7 0bc8cbf1
...@@ -379,13 +379,12 @@ class ActiveRecord extends Model ...@@ -379,13 +379,12 @@ class ActiveRecord extends Model
} elseif ($this->hasAttribute($name)) { } elseif ($this->hasAttribute($name)) {
return null; return null;
} else { } else {
$t = strtolower($name); if (isset($this->_related[$name]) || array_key_exists($name, $this->_related)) {
if (isset($this->_related[$t]) || array_key_exists($t, $this->_related)) { return $this->_related[$name];
return $this->_related[$t];
} }
$value = parent::__get($name); $value = parent::__get($name);
if ($value instanceof ActiveRelation) { 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 { } else {
return $value; return $value;
} }
...@@ -433,9 +432,8 @@ class ActiveRecord extends Model ...@@ -433,9 +432,8 @@ class ActiveRecord extends Model
if ($this->hasAttribute($name)) { if ($this->hasAttribute($name)) {
unset($this->_attributes[$name]); unset($this->_attributes[$name]);
} else { } else {
$t = strtolower($name); if (isset($this->_related[$name])) {
if (isset($this->_related[$t])) { unset($this->_related[$name]);
unset($this->_related[$t]);
} else { } else {
parent::__unset($name); parent::__unset($name);
} }
...@@ -523,12 +521,31 @@ class ActiveRecord extends Model ...@@ -523,12 +521,31 @@ class ActiveRecord extends Model
/** /**
* Populates the named relation with the related records. * Populates the named relation with the related records.
* Note that this method does not check if the relation exists or not. * 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. * @param ActiveRecord|array|null the related records to be populated into the relation.
*/ */
public function populateRelation($name, $records) 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;
} }
/** /**
...@@ -1286,7 +1303,7 @@ class ActiveRecord extends Model ...@@ -1286,7 +1303,7 @@ class ActiveRecord extends Model
* *
* Note that this method requires that the primary key value is not null. * 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 ActiveRecord $model the model to be linked with the current one.
* @param array $extraColumns additional column values to be saved into the pivot table. * @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 * This parameter is only meaningful for a relationship involving a pivot table
...@@ -1308,7 +1325,7 @@ class ActiveRecord extends Model ...@@ -1308,7 +1325,7 @@ class ActiveRecord extends Model
$viaClass = $viaRelation->modelClass; $viaClass = $viaRelation->modelClass;
$viaTable = $viaClass::tableName(); $viaTable = $viaClass::tableName();
// unset $viaName so that it can be reloaded to reflect the change // unset $viaName so that it can be reloaded to reflect the change
unset($this->_related[strtolower($viaName)]); unset($this->_related[$viaName]);
} else { } else {
$viaRelation = $relation->via; $viaRelation = $relation->via;
$viaTable = reset($relation->via->from); $viaTable = reset($relation->via->from);
...@@ -1364,7 +1381,7 @@ class ActiveRecord extends Model ...@@ -1364,7 +1381,7 @@ class ActiveRecord extends Model
* The model with the foreign key of the relationship will be deleted if `$delete` is true. * 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. * 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 ActiveRecord $model the model to be unlinked from the current one.
* @param boolean $delete whether to delete the model that contains the foreign key. * @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. * If false, the model's foreign key will be set null and saved.
...@@ -1382,7 +1399,7 @@ class ActiveRecord extends Model ...@@ -1382,7 +1399,7 @@ class ActiveRecord extends Model
/** @var $viaClass ActiveRecord */ /** @var $viaClass ActiveRecord */
$viaClass = $viaRelation->modelClass; $viaClass = $viaRelation->modelClass;
$viaTable = $viaClass::tableName(); $viaTable = $viaClass::tableName();
unset($this->_related[strtolower($viaName)]); unset($this->_related[$viaName]);
} else { } else {
$viaRelation = $relation->via; $viaRelation = $relation->via;
$viaTable = reset($relation->via->from); $viaTable = reset($relation->via->from);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment