{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/15875","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/15875/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/15875/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/15875/events","html_url":"https://github.com/yiisoft/yii2/issues/15875","id":304168485,"number":15875,"title":"afterSave for new models flushes unsaved data","user":{"login":"yujin1st","id":2317913,"avatar_url":"https://avatars1.githubusercontent.com/u/2317913?v=4","gravatar_id":"","url":"https://api.github.com/users/yujin1st","html_url":"https://github.com/yujin1st","followers_url":"https://api.github.com/users/yujin1st/followers","following_url":"https://api.github.com/users/yujin1st/following{/other_user}","gists_url":"https://api.github.com/users/yujin1st/gists{/gist_id}","starred_url":"https://api.github.com/users/yujin1st/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yujin1st/subscriptions","organizations_url":"https://api.github.com/users/yujin1st/orgs","repos_url":"https://api.github.com/users/yujin1st/repos","events_url":"https://api.github.com/users/yujin1st/events{/privacy}","received_events_url":"https://api.github.com/users/yujin1st/received_events","type":"User","site_admin":false},"labels":[{"id":92646963,"url":"https://api.github.com/repos/yiisoft/yii2/labels/severity:important","name":"severity:important","color":"eb6420","default":false},{"id":33208443,"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:bug","name":"type:bug","color":"e102d8","default":false}],"state":"open","locked":false,"assignee":{"login":"cebe","id":189796,"avatar_url":"https://avatars0.githubusercontent.com/u/189796?v=4","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},"assignees":[{"login":"cebe","id":189796,"avatar_url":"https://avatars0.githubusercontent.com/u/189796?v=4","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/25","html_url":"https://github.com/yiisoft/yii2/milestone/25","labels_url":"https://api.github.com/repos/yiisoft/yii2/milestones/25/labels","id":2562828,"number":25,"title":"2.0.15","description":"","creator":{"login":"cebe","id":189796,"avatar_url":"https://avatars0.githubusercontent.com/u/189796?v=4","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},"open_issues":154,"closed_issues":8,"state":"open","created_at":"2017-06-05T18:03:36Z","updated_at":"2018-03-14T11:32:55Z","due_on":"2018-03-20T07:00:00Z","closed_at":null},"comments":9,"created_at":"2018-03-11T15:45:01Z","updated_at":"2018-03-14T20:59:18Z","closed_at":null,"author_association":"NONE","body":"After #13567 AR now flushed relational data, so there is new issue:\r\n\r\n### What steps will reproduce the problem?\r\n1. Create simple app with Order and OrderItem models.\r\n2. Load data in controller, create relational OrderItem records and populate relation 'items'\r\n```php\r\n// In Order \r\npublic function load($data, $formName = null) {\r\n  if (!parent::load($data, $formName)) return false;\r\n  $this->loadItems($data);\r\n  return true;\r\n}\r\n\r\n/// loading and populating data\r\nprivate function loadItems($data) {\r\n  if (!isset($data['items'])) {\r\n    return;\r\n  }\r\n  $items = $this->items;\r\n  foreach ($data['items'] as $info) {\r\n    $itemId = $info['itemId'];\r\n    if (isset($items[$itemId])) {\r\n      $items[$itemId]->load($info, '');\r\n    } else {\r\n      $item = new OrderItem([]);\r\n      $item->load($info, '');\r\n      $items[$itemId] = $item;\r\n    }\r\n  }\r\n  $this->populateRelation('items',$items);\r\n}\r\n\r\n\r\n```\r\n\r\n3. Validate, do something in beforeSave method and event\r\n3.1 Validate in Order model\r\n3.2 For example warehouse module can check item quantity before order creating in event\r\n4. Save items in afterSave\r\n```php\r\npublic function afterSave($insert, $changedAttributes) {\r\n  $this->saveItems();\r\n  parent::afterSave($insert, $changedAttributes);\r\n}\r\n\r\n```\r\n\r\n5. Do something with new items in afterSave method and event\r\n \r\n### What is the expected result?\r\nAll works fine, order data saved, relational data saved too\r\n\r\n### What do you get instead?\r\nAfter #13567 items relation with unfinished data is flushed, as Order model changes it's id, so you lose data\r\n\r\n### Additional info\r\nIt simple workflow allows to validate and process relational form data as ActiveRecord models with all it features and controllers remains thing. And you could use scenarios for complex relations, so models can be easily tested.\r\n\r\nI'm afraid this issue will be closed due contradiction with updated framework design, but i have a multiply applications that follows this workflow, so i don't know what to do ;(((\r\n\r\n| Q                | A\r\n| ---------------- | ---\r\n| Yii version      | 2.0.14\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/373172711","html_url":"https://github.com/yiisoft/yii2/issues/15875#issuecomment-373172711","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/15875","id":373172711,"user":{"login":"samdark","id":47294,"avatar_url":"https://avatars2.githubusercontent.com/u/47294?v=4","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"created_at":"2018-03-14T20:59:18Z","updated_at":"2018-03-14T20:59:18Z","author_association":"OWNER","body":"I guess in this case we should not reset relations and your extra condition may fix it."}}
{"actor":{"display_login":"samdark"}}