Commit 90839ceb by Carsten Brandt

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
parent d8b94d64
......@@ -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.
......
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