{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/7040","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/7040/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/7040/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/7040/events","html_url":"https://github.com/yiisoft/yii2/issues/7040","id":55579048,"number":7040,"title":"Using Model::loadMultiple to update records in db correctly","user":{"login":"kartik-v","id":3592619,"avatar_url":"https://avatars.githubusercontent.com/u/3592619?v=3","gravatar_id":"","url":"https://api.github.com/users/kartik-v","html_url":"https://github.com/kartik-v","followers_url":"https://api.github.com/users/kartik-v/followers","following_url":"https://api.github.com/users/kartik-v/following{/other_user}","gists_url":"https://api.github.com/users/kartik-v/gists{/gist_id}","starred_url":"https://api.github.com/users/kartik-v/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kartik-v/subscriptions","organizations_url":"https://api.github.com/users/kartik-v/orgs","repos_url":"https://api.github.com/users/kartik-v/repos","events_url":"https://api.github.com/users/kartik-v/events{/privacy}","received_events_url":"https://api.github.com/users/kartik-v/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:docs","name":"type:docs","color":"207de5"}],"state":"open","locked":false,"assignee":{"login":"cebe","id":189796,"avatar_url":"https://avatars.githubusercontent.com/u/189796?v=3","gravatar_id":"","url":"https://api.github.com/users/cebe","html_url":"https://github.com/cebe","followers_url":"https://api.github.com/users/cebe/followers","following_url":"https://api.github.com/users/cebe/following{/other_user}","gists_url":"https://api.github.com/users/cebe/gists{/gist_id}","starred_url":"https://api.github.com/users/cebe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cebe/subscriptions","organizations_url":"https://api.github.com/users/cebe/orgs","repos_url":"https://api.github.com/users/cebe/repos","events_url":"https://api.github.com/users/cebe/events{/privacy}","received_events_url":"https://api.github.com/users/cebe/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/yiisoft/yii2/milestones/11","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/11/labels","id":896106,"number":11,"title":"2.0.3","description":"","creator":{"login":"qiangxue","id":993322,"avatar_url":"https://avatars.githubusercontent.com/u/993322?v=3","gravatar_id":"","url":"https://api.github.com/users/qiangxue","html_url":"https://github.com/qiangxue","followers_url":"https://api.github.com/users/qiangxue/followers","following_url":"https://api.github.com/users/qiangxue/following{/other_user}","gists_url":"https://api.github.com/users/qiangxue/gists{/gist_id}","starred_url":"https://api.github.com/users/qiangxue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qiangxue/subscriptions","organizations_url":"https://api.github.com/users/qiangxue/orgs","repos_url":"https://api.github.com/users/qiangxue/repos","events_url":"https://api.github.com/users/qiangxue/events{/privacy}","received_events_url":"https://api.github.com/users/qiangxue/received_events","type":"User","site_admin":false},"open_issues":30,"closed_issues":77,"state":"open","created_at":"2014-12-08T18:26:37Z","updated_at":"2015-01-27T09:37:36Z","due_on":"2015-03-01T05:00:00Z","closed_at":null},"comments":14,"created_at":"2015-01-27T05:53:05Z","updated_at":"2015-01-27T09:49:43Z","closed_at":null,"body":"Probably a basic question - but is critical for updating the right records to the db.\r\n\r\nHow will the `Model::loadMultiple` fetch the right record for update - because I do not see this linked to a primary key. To illustrate let's say we have just two columns `id`, and `name` (`id` being the pkey). If we send an array from form post... where there are multiple records with the same `name` value, how will loadMultiple assign it to the correct `id`?\r\n\r\nTo make it complex let's say the records are NOT sorted by id when the array is passed. \r\n\r\nI suppose the primary key must be passed with the POST for it to happen automatically. If not there will be an issue ain't it?"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/71618039","html_url":"https://github.com/yiisoft/yii2/issues/7040#issuecomment-71618039","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/7040","id":71618039,"user":{"login":"kartik-v","id":3592619,"avatar_url":"https://avatars.githubusercontent.com/u/3592619?v=3","gravatar_id":"","url":"https://api.github.com/users/kartik-v","html_url":"https://github.com/kartik-v","followers_url":"https://api.github.com/users/kartik-v/followers","following_url":"https://api.github.com/users/kartik-v/following{/other_user}","gists_url":"https://api.github.com/users/kartik-v/gists{/gist_id}","starred_url":"https://api.github.com/users/kartik-v/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kartik-v/subscriptions","organizations_url":"https://api.github.com/users/kartik-v/orgs","repos_url":"https://api.github.com/users/kartik-v/repos","events_url":"https://api.github.com/users/kartik-v/events{/privacy}","received_events_url":"https://api.github.com/users/kartik-v/received_events","type":"User","site_admin":false},"created_at":"2015-01-27T09:49:43Z","updated_at":"2015-01-27T09:49:43Z","body":"I think I get it ... after using `indexBy` you need to probably add the index to the `name` of the form fields to generate the array. So the client reordering issue will not have any impact. Is that it?\r\n\r\nSomething like the following will need to be generated for the field name:\r\n```php\r\nModel[$index]['attribute']\r\n```\r\n\r\nwhere $index will automatically match the primary key. Then this is cool.\r\n\r\nSummarizing:\r\n\r\n```php\r\n// your query \r\n$query = Model::find()->indexBy('id'); // where `id` is your primary key\r\n\r\n$dataProvider = new ActiveDataProvider([\r\n    'query' => $query,\r\n]);\r\n\r\n// in form\r\nforeach($dataProvider->getModels() as $index=>$model) {\r\n    echo $form->field($model, \"[$index]name\");\r\n}\r\n```"}}