diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index f6403d7ee2..f97bad96a0 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -1476,6 +1476,45 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface return $this->generateAttributeLabel($attribute); } + /** + * Returns the text hint for the specified attribute. + * If the attribute looks like `relatedModel.attribute`, then the attribute will be received from the related model. + * @param string $attribute the attribute name + * @return string the attribute hint + * @see attributeHints() + * @since 2.0.4 + */ + public function getAttributeHint($attribute) + { + $hints = $this->attributeHints(); + if (isset($hints[$attribute])) { + return ($hints[$attribute]); + } elseif (strpos($attribute, '.')) { + $attributeParts = explode('.', $attribute); + $neededAttribute = array_pop($attributeParts); + + $relatedModel = $this; + foreach ($attributeParts as $relationName) { + if (isset($this->_related[$relationName]) && $this->_related[$relationName] instanceof self) { + $relatedModel = $this->_related[$relationName]; + } else { + try { + $relation = $relatedModel->getRelation($relationName); + } catch (InvalidParamException $e) { + return ''; + } + $relatedModel = new $relation->modelClass; + } + } + + $hints = $relatedModel->attributeHints(); + if (isset($hints[$neededAttribute])) { + return $hints[$neededAttribute]; + } + } + return ''; + } + /** * @inheritdoc *