Permalink
Browse files

Fixes #1457: support using AR relations as input.

  • Loading branch information...
qiangxue committed Dec 7, 2013
1 parent a5188f1 commit 1f5f19df13036d66aed3fb74689cbefab425dc90
Showing with 30 additions and 27 deletions.
  1. +0 −11 framework/yii/db/BaseActiveRecord.php
  2. +30 −16 framework/yii/helpers/BaseHtml.php
@@ -92,17 +92,6 @@
private $_related = [];
- /**
- * 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.
*
@@ -9,6 +9,7 @@
use Yii;
use yii\base\InvalidParamException;
+use yii\db\ActiveRecordInterface;
use yii\web\Request;
use yii\base\Model;
@@ -1208,11 +1209,11 @@ public static function activeCheckbox($model, $attribute, $options = [])
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 @@ public static function activeDropDownList($model, $attribute, $items, $options =
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 @@ public static function activeListBox($model, $attribute, $items, $options = [])
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 @@ public static function activeCheckboxList($model, $attribute, $items, $options =
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 @@ public static function getAttributeName($attribute)
* 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 @@ public static function getAttributeValue($model, $attribute)
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) {
+ $value = $model->$attribute;
+ 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;
}
/**

0 comments on commit 1f5f19d

Please sign in to comment.