public function getRelation($name, $throwException = true)
{
$getter = 'get' . $name;
try {
// the relation could be defined in a behavior
$relation = $this->{$getter}();
} catch (UnknownMethodException $e) {
if ($throwException) {
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
} else {
return null;
}
}
if (!$relation instanceof ActiveQueryInterface) {
if ($throwException) {
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".');
} else {
return null;
}
}
if (method_exists($this, $getter)) {
// relation name is case sensitive, trying to validate it when the relation is defined within this class
$method = new \ReflectionMethod($this, $getter);
$realName = lcfirst(substr($method->getName(), 3));
if ($realName !== $name) {
if ($throwException) {
throw new InvalidParamException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"{$realName}\" instead of \"{$name}\".");
} else {
return null;
}
}
}
return $relation;
}