{"action":"closed","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/11270","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/11270/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/11270/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/11270/events","html_url":"https://github.com/yiisoft/yii2/issues/11270","id":145698427,"number":11270,"title":"Error when link record in one to many relation","user":{"login":"iushev","id":3023174,"avatar_url":"https://avatars.githubusercontent.com/u/3023174?v=3","gravatar_id":"","url":"https://api.github.com/users/iushev","html_url":"https://github.com/iushev","followers_url":"https://api.github.com/users/iushev/followers","following_url":"https://api.github.com/users/iushev/following{/other_user}","gists_url":"https://api.github.com/users/iushev/gists{/gist_id}","starred_url":"https://api.github.com/users/iushev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iushev/subscriptions","organizations_url":"https://api.github.com/users/iushev/orgs","repos_url":"https://api.github.com/users/iushev/repos","events_url":"https://api.github.com/users/iushev/events{/privacy}","received_events_url":"https://api.github.com/users/iushev/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/status:ready%20for%20adoption","name":"status:ready for adoption","color":"02e10c"},{"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:bug","name":"type:bug","color":"e102d8"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-04T14:21:25Z","updated_at":"2016-04-08T07:36:51Z","closed_at":"2016-04-08T07:36:51Z","body":"Hi\r\nI have one active record that have one to many relation with other one.\r\nIn my primary model relation is defined like this:\r\n```php\r\nclass Product extends ActiveRecord\r\n{\r\n...\r\n    public function getProductOptions()\r\n    {\r\n        $query = $this->hasMany(ProductOption::className(), ['product_id' => 'id']);\r\n        $query->indexBy(function ($row) {\r\n            return $row->option->key_name . '_' . $row->id;\r\n        });\r\n        return $query;\r\n    }\r\n...\r\n}\r\n```\r\nWhen save data i link ```ProductOption``` with ```Product```\r\n```php\r\n$product->link('productOptions', $productOption);\r\n```\r\nand this throw exception \"Object of class Closure could not be converted to string\".\r\nThe exception is throw from 'vendor/yiisoft/yii2/db/BaseActiveRecord.php' at row 1239: \r\n```php\r\n        // update lazily loaded related objects\r\n        if (!$relation->multiple) {\r\n            $this->_related[$name] = $model;\r\n        } elseif (isset($this->_related[$name])) {\r\n            if ($relation->indexBy !== null) {\r\n                $indexBy = $relation->indexBy;\r\n                $this->_related[$name][$model->$indexBy] = $model;  // this row throw exception\r\n            } else {\r\n                $this->_related[$name][] = $model;\r\n            }\r\n        }\r\n```\r\nThe error is occur because i have ```indexBy``` with function. In my case I need this.\r\n\r\n| Q                | A\r\n| ---------------- | ---\r\n| Yii version      |  2.0.7\r\n| PHP version      |  5.5.9-1ubuntu4.14\r\n| Operating system | Linux 3.13.0-68-generic #111-Ubuntu SMP Fri Nov 6 18:17:06 UTC 2015 x86_64\r\n"}}