{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/8878","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/8878/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/8878/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/8878/events","html_url":"https://github.com/yiisoft/yii2/issues/8878","id":90423657,"number":8878,"title":"$this->id is the incorrect ID","user":{"login":"synackSA","id":5345876,"avatar_url":"https://avatars.githubusercontent.com/u/5345876?v=3","gravatar_id":"","url":"https://api.github.com/users/synackSA","html_url":"https://github.com/synackSA","followers_url":"https://api.github.com/users/synackSA/followers","following_url":"https://api.github.com/users/synackSA/following{/other_user}","gists_url":"https://api.github.com/users/synackSA/gists{/gist_id}","starred_url":"https://api.github.com/users/synackSA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/synackSA/subscriptions","organizations_url":"https://api.github.com/users/synackSA/orgs","repos_url":"https://api.github.com/users/synackSA/repos","events_url":"https://api.github.com/users/synackSA/events{/privacy}","received_events_url":"https://api.github.com/users/synackSA/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/yiisoft/yii2/labels/status:to%20be%20verified","name":"status:to be verified","color":"fbca04"},{"url":"https://api.github.com/repos/yiisoft/yii2/labels/type:bug","name":"type:bug","color":"e102d8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-23T15:46:02Z","updated_at":"2015-06-23T17:45:06Z","closed_at":null,"body":"So a problem has recently popped up for me when I have logging to the DB enabled. If I do a ActiveRecord save() and then reference the $id for that record afterwards, it has the wrong id. For instance with the following code:\r\n\r\n    public function saveStudent($first_name, $last_name, $gender)\r\n    {\r\n        $student = new Student();\r\n        $student->first_name = $first_name;\r\n        $student->last_name = $last_name;\r\n        $student->gender = $gender;\r\n\r\n        Yii::info(\"Saving student: {$student->first_name} {$student->last_name}\");\r\n        $student->save();\r\n\r\n        return $student->id;\r\n    }\r\n\r\nUses the following class:\r\n\r\n    class Student extends \\yii\\db\\ActiveRecord \r\n    {\r\n\r\n        /* NORMAL CODE HERE */\r\n\r\n        public class afterSave($insert, $changedAttributes)\r\n        {\r\n            parent::afterSave($insert, $changedAttributes);\r\n\r\n            Class::assignClass($this->id);\r\n        }\r\n\r\n    }\r\n\r\n\r\nThe ID that is passed into `Class::assignClass()` is not the ID for the student record, but rather the ID for the record for the log entry that was written to the log table in the DB for the line `Yii::info(\"Saving student: {$student->first_name} {$student->last_name}\");`. This doesn't always happen, but it is happening and it's very annoying. To be clear, the `Student` record is being saved into the DB, as I checked to make sure that the record was there. Here is my config for the writing of the logs to the DB.\r\n\r\n                   [\r\n                    'class' => 'yii\\log\\DbTarget',\r\n                    'exportInterval' => 10,\r\n                    'levels' => ['error', 'warning', 'info'],\r\n                    'logVars' => ['_COOKIE',],\r\n                    'categories' => [\r\n                        'sync',\r\n                        'treb',\r\n                        'sync_cleanup',\r\n                        'application'\r\n                    ],\r\n\r\n~~I've written the following code into my `afterSave()` of one of my classes, only to have it happen elsewhere. I don't want to have to write this code all over.~~ Ignore this, the code doesn't work as intended.\r\n\r\n    public function afterSave($insert, $changedAttributes)\r\n    {\r\n        parent::afterSave($insert, $changedAttributes);\r\n        $this->refresh();\r\n\r\n        $lastID = $this->db->getLastInsertID();\r\n        if ($lastID != $this->id) {\r\n            Yii::warning(\"ERROR: Record ID ({$this->id}) is not equal to last inserted ID ($lastID), reassigning\");\r\n            $this->id = $lastID;\r\n        }\r\n    }\r\n\r\nPlease note that the `$this->refresh()` is not fixing the issue."},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/114586490","html_url":"https://github.com/yiisoft/yii2/issues/8878#issuecomment-114586490","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/8878","id":114586490,"user":{"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},"created_at":"2015-06-23T17:45:06Z","updated_at":"2015-06-23T17:45:06Z","body":"which version of yii are you using?"}}