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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
1f5f19df
Commit
1f5f19df
authored
Dec 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1457: support using AR relations as input.
parent
a5188f1f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
26 deletions
+29
-26
BaseActiveRecord.php
framework/yii/db/BaseActiveRecord.php
+0
-11
BaseHtml.php
framework/yii/helpers/BaseHtml.php
+29
-15
No files found.
framework/yii/db/BaseActiveRecord.php
View file @
1f5f19df
...
...
@@ -93,17 +93,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
/**
* Returns the string representation of this AR instance.
* The default implementation will return the primary key (or JSON representation of the primary key if it is composite).
* @return string the string representation of this AR instance.
*/
public
function
__toString
()
{
$pk
=
$this
->
getPrimaryKey
();
return
is_array
(
$pk
)
?
json_encode
(
$pk
)
:
$pk
;
}
/**
* Creates an [[ActiveQuery]] instance for query purpose.
*
* @include @yii/db/ActiveRecord-find.md
...
...
framework/yii/helpers/BaseHtml.php
View file @
1f5f19df
...
...
@@ -9,6 +9,7 @@ namespace yii\helpers;
use
Yii
;
use
yii\base\InvalidParamException
;
use
yii\db\ActiveRecordInterface
;
use
yii\web\Request
;
use
yii\base\Model
;
...
...
@@ -1208,11 +1209,11 @@ class BaseHtml
public
static
function
activeDropDownList
(
$model
,
$attribute
,
$items
,
$options
=
[])
{
$name
=
isset
(
$options
[
'name'
])
?
$options
[
'name'
]
:
static
::
getInputName
(
$model
,
$attribute
);
$
checked
=
static
::
getAttributeValue
(
$model
,
$attribute
);
$
selection
=
static
::
getAttributeValue
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
static
::
getInputId
(
$model
,
$attribute
);
}
return
static
::
dropDownList
(
$name
,
$
checked
,
$items
,
$options
);
return
static
::
dropDownList
(
$name
,
$
selection
,
$items
,
$options
);
}
/**
...
...
@@ -1256,14 +1257,14 @@ class BaseHtml
public
static
function
activeListBox
(
$model
,
$attribute
,
$items
,
$options
=
[])
{
$name
=
isset
(
$options
[
'name'
])
?
$options
[
'name'
]
:
static
::
getInputName
(
$model
,
$attribute
);
$
checked
=
static
::
getAttributeValue
(
$model
,
$attribute
);
$
selection
=
static
::
getAttributeValue
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
static
::
getInputId
(
$model
,
$attribute
);
}
return
static
::
listBox
(
$name
,
$
checked
,
$items
,
$options
);
return
static
::
listBox
(
$name
,
$
selection
,
$items
,
$options
);
}
/**
...
...
@@ -1297,14 +1298,14 @@ class BaseHtml
public
static
function
activeCheckboxList
(
$model
,
$attribute
,
$items
,
$options
=
[])
{
$name
=
isset
(
$options
[
'name'
])
?
$options
[
'name'
]
:
static
::
getInputName
(
$model
,
$attribute
);
$
checked
=
static
::
getAttributeValue
(
$model
,
$attribute
);
$
selection
=
static
::
getAttributeValue
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
static
::
getInputId
(
$model
,
$attribute
);
}
return
static
::
checkboxList
(
$name
,
$
checked
,
$items
,
$options
);
return
static
::
checkboxList
(
$name
,
$
selection
,
$items
,
$options
);
}
/**
...
...
@@ -1337,14 +1338,14 @@ class BaseHtml
public
static
function
activeRadioList
(
$model
,
$attribute
,
$items
,
$options
=
[])
{
$name
=
isset
(
$options
[
'name'
])
?
$options
[
'name'
]
:
static
::
getInputName
(
$model
,
$attribute
);
$
checked
=
static
::
getAttributeValue
(
$model
,
$attribute
);
$
selection
=
static
::
getAttributeValue
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
static
::
getInputId
(
$model
,
$attribute
);
}
return
static
::
radioList
(
$name
,
$
checked
,
$items
,
$options
);
return
static
::
radioList
(
$name
,
$
selection
,
$items
,
$options
);
}
/**
...
...
@@ -1546,9 +1547,12 @@ class BaseHtml
* For an attribute expression like `[0]dates[0]`, this method will return the value of `$model->dates[0]`.
* See [[getAttributeName()]] for more details about attribute expression.
*
* If an attribute value is an instance of [[ActiveRecordInterface]] or an array of such instances,
* the primary value(s) of the AR instance(s) will be returned instead.
*
* @param Model $model the model object
* @param string $attribute the attribute name or expression
* @return
mixed
the corresponding attribute value
* @return
string|array
the corresponding attribute value
* @throws InvalidParamException if the attribute name contains non-word characters.
*/
public
static
function
getAttributeValue
(
$model
,
$attribute
)
...
...
@@ -1557,20 +1561,30 @@ class BaseHtml
throw
new
InvalidParamException
(
'Attribute name must contain word characters only.'
);
}
$attribute
=
$matches
[
2
];
$index
=
$matches
[
3
];
if
(
$index
===
''
)
{
return
$model
->
$attribute
;
}
else
{
$value
=
$model
->
$attribute
;
foreach
(
explode
(
']['
,
trim
(
$index
,
'[]'
))
as
$id
)
{
if
(
$matches
[
3
]
!==
''
)
{
foreach
(
explode
(
']['
,
trim
(
$matches
[
3
],
'[]'
))
as
$id
)
{
if
((
is_array
(
$value
)
||
$value
instanceof
\ArrayAccess
)
&&
isset
(
$value
[
$id
]))
{
$value
=
$value
[
$id
];
}
else
{
return
null
;
}
}
return
$value
;
}
// https://github.com/yiisoft/yii2/issues/1457
if
(
is_array
(
$value
))
{
foreach
(
$value
as
$i
=>
$v
)
{
if
(
$v
instanceof
ActiveRecordInterface
)
{
$v
=
$v
->
getPrimaryKey
(
false
);
$value
[
$i
]
=
is_array
(
$v
)
?
json_encode
(
$v
)
:
$v
;
}
}
}
elseif
(
$value
instanceof
ActiveRecordInterface
)
{
$value
=
$value
->
getPrimaryKey
(
false
);
return
is_array
(
$value
)
?
json_encode
(
$value
)
:
$value
;
}
return
$value
;
}
/**
...
...
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