Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
10e7bca3
Commit
10e7bca3
authored
Feb 15, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2443 from djagya/2436-related-model-attributes
#2436 receive label of the attribute from related model
parents
22bfd431
d2707364
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
0 deletions
+38
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+37
-0
No files found.
framework/CHANGELOG.md
View file @
10e7bca3
...
...
@@ -111,6 +111,7 @@ Yii Framework 2 Change Log
-
Enh #2364: Take into account current error reporting level in error handler (gureedo)
-
Enh #2387: Added support for fetching data from database in batches (nineinchnick, qiangxue)
-
Enh #2417: Added possibility to set
`dataType`
for
`$.ajax`
call in yii.activeForm.js (Borales)
-
Enh #2436: Label of the attribute, which looks like
`relatedModel.attribute`
, will be received from the related model if it available (djagya)
-
Enh: Added support for using arrays as option values for console commands (qiangxue)
-
Enh: Added
`favicon.ico`
and
`robots.txt`
to default application templates (samdark)
-
Enh: Added
`Widget::autoIdPrefix`
to support prefixing automatically generated widget IDs (qiangxue)
...
...
framework/db/BaseActiveRecord.php
View file @
10e7bca3
...
...
@@ -1286,4 +1286,41 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
return
false
;
}
}
/**
* Returns the text label 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 label
* @see generateAttributeLabel()
* @see attributeLabels()
*/
public
function
getAttributeLabel
(
$attribute
)
{
$labels
=
$this
->
attributeLabels
();
if
(
isset
(
$labels
[
$attribute
]))
{
return
(
$labels
[
$attribute
]);
}
elseif
(
strpos
(
$attribute
,
'.'
))
{
$attributeParts
=
explode
(
'.'
,
$attribute
);
$neededAttribute
=
array_pop
(
$attributeParts
);
$relatedModel
=
$this
;
foreach
(
$attributeParts
as
$relationName
)
{
try
{
$relation
=
$relatedModel
->
getRelation
(
$relationName
);
}
catch
(
InvalidParamException
$e
)
{
return
$this
->
generateAttributeLabel
(
$attribute
);
}
$relatedModel
=
new
$relation
->
modelClass
;
}
$labels
=
$relatedModel
->
attributeLabels
();
if
(
isset
(
$labels
[
$neededAttribute
]))
{
return
$labels
[
$neededAttribute
];
}
}
return
$this
->
generateAttributeLabel
(
$attribute
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment