{"action":"closed","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9519","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9519/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9519/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9519/events","html_url":"https://github.com/yiisoft/yii2/issues/9519","id":103187314,"number":9519,"title":"yii\\helpers\\BaseHtml trouble getting attribute label in model with array attribute","user":{"login":"surgat","id":7720715,"avatar_url":"https://avatars.githubusercontent.com/u/7720715?v=3","gravatar_id":"","url":"https://api.github.com/users/surgat","html_url":"https://github.com/surgat","followers_url":"https://api.github.com/users/surgat/followers","following_url":"https://api.github.com/users/surgat/following{/other_user}","gists_url":"https://api.github.com/users/surgat/gists{/gist_id}","starred_url":"https://api.github.com/users/surgat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/surgat/subscriptions","organizations_url":"https://api.github.com/users/surgat/orgs","repos_url":"https://api.github.com/users/surgat/repos","events_url":"https://api.github.com/users/surgat/events{/privacy}","received_events_url":"https://api.github.com/users/surgat/received_events","type":"User","site_admin":false},"labels":[{"id":33208430,"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:feature","name":"type:feature","color":"02d7e1","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2015-08-26T06:33:34Z","updated_at":"2016-11-14T22:38:36Z","closed_at":"2016-11-14T22:38:36Z","body":"We have record with embedded model(or array)\n\n``` php\nclass Project extends ActiveRecord\n{\n    /**\n     * @return FeedModel\n     */\n    public function getFeed();\n\n    /**\n     * @param FeedModel $model\n     */\n    public function setFeed($model);\n\n    public function attributeLabels()\n    {\n        return [\n            'name'      => 'Name',\n            'feed.url'  => 'Url'\n        ];\n    }\n}\n\nclass FeedModel implements Model\n{\n    public $url;\n}\n```\n# Then\n## Firts. Access with [ ]\n\n``` php\n// return label name \"Feed\". Label feed.url not working\necho $form->field($model, 'feed[url]')->dropDownList(FeedModel::typeList());\n```\n\nTo work here need copy the code from yii\\helpers\\BaseHtml::getAttributeValue\n\n``` php\n................................\n        if ($matches[3] !== '') {\n            foreach (explode('][', trim($matches[3], '[]')) as $id) {\n                if ((is_array($value) || $value instanceof \\ArrayAccess) && isset($value[$id])) {\n                    $value = $value[$id];\n                } else {\n                    return null;\n                }\n            }\n        }\n................................\n```\n\nTo yii\\helpers\\BaseHtml::getAttributeName and replace [] on .\n\n``` php\n    public static function getAttributeName($attribute)\n    {\n        if (preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {\n            $attribute = [$matches[2]];\n            if ($matches[3] !== '') {    // Here\n                $attribute = array_merge($attribute, explode('][', trim($matches[3], '[]')));\n            }\n            return implode('.', $attribute);\n        } else {\n            throw new InvalidParamException('Attribute name must contain word characters only.');\n        }\n    }\n```\n## Second. Access with \".\"\n\n``` php\n// PHP User Error – yii\\base\\ErrorException \necho $form->field($model, 'feed.url')->dropDownList(FeedModel::typeList());\n```\n\nChar \".\" allowed, but not dereferenced as array access in yii\\grid\\DataColumn\n\nIn yii\\helpers\\BaseHtml\n\n``` php\n    public static function getAttributeValue($model, $attribute)\n    {\n        if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {\n            throw new InvalidParamException('Attribute name must contain word characters only.');\n        }\n        $attribute = $matches[2];\n        $value = $model->$attribute; // This\n        ....................................\n    }\n```\n\nCan replace\n\n``` php\n    public static function getAttributeValue($model, $attribute)\n    {\n        if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {\n            throw new InvalidParamException('Attribute name must contain word characters only.');\n        }\n        $value = ArrayHelper::getValue($model, $matches[2]);\n        ....................................................\n    }\n```\n\nBut input name will be feed.type\n\n``` php\n    public static function getInputName($model, $attribute)\n    {\n        $formName = $model->formName();\n        if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {\n            throw new InvalidParamException('Attribute name must contain word characters only.');\n        }\n        $prefix = $matches[1];\n        $attribute = $matches[2];\n        $suffix = $matches[3];\n        if ($formName === '' && $prefix === '') {\n            return $attribute . $suffix;\n        } elseif ($formName !== '') {\n            return $formName . $prefix . \"[$attribute]\" . $suffix;\n        } else {\n            throw new InvalidParamException(get_class($model) . '::formName() cannot be empty for tabular inputs.');\n        }\n    }\n```\n\nCan replace\n\n``` php\n    public static function getInputName($model, $attribute)\n    {\n        $formName = $model->formName();\n        if (!preg_match('/(^|.*\\])([\\w\\.]+)(\\[.*|$)/', $attribute, $matches)) {\n            throw new InvalidParamException('Attribute name must contain word characters only.');\n        }\n        $prefix = $matches[1];\n        $attribute = $matches[2];\n        $suffix = $matches[3];\n        $secondAttributes = '';\n        if (strpos($attribute, '.')) {\n            $list = explode('.', $attribute);\n            $attribute = array_shift($list);\n            $secondAttributes = '[' . implode('][', $list) . ']';\n        }\n        if ($formName === '' && $prefix === '') {\n            return $attribute . $secondAttributes . $suffix;\n        } elseif ($formName !== '') {\n            return $formName . $prefix . \"[$attribute]\" . $secondAttributes . $suffix;\n        } else {\n            throw new InvalidParamException(get_class($model) . '::formName() cannot be empty for tabular inputs.');\n        }\n    }\n```\n"}}
{"actor":{"display_login":"cebe"}}