{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/13805","repository_url":"https://api.github.com/repos/yiisoft/yii2","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/13805/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/13805/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/13805/events","html_url":"https://github.com/yiisoft/yii2/issues/13805","id":215329200,"number":13805,"title":"Error when link model","user":{"login":"dkduyanh","id":10934695,"avatar_url":"https://avatars3.githubusercontent.com/u/10934695?v=3","gravatar_id":"","url":"https://api.github.com/users/dkduyanh","html_url":"https://github.com/dkduyanh","followers_url":"https://api.github.com/users/dkduyanh/followers","following_url":"https://api.github.com/users/dkduyanh/following{/other_user}","gists_url":"https://api.github.com/users/dkduyanh/gists{/gist_id}","starred_url":"https://api.github.com/users/dkduyanh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkduyanh/subscriptions","organizations_url":"https://api.github.com/users/dkduyanh/orgs","repos_url":"https://api.github.com/users/dkduyanh/repos","events_url":"https://api.github.com/users/dkduyanh/events{/privacy}","received_events_url":"https://api.github.com/users/dkduyanh/received_events","type":"User","site_admin":false},"labels":[{"id":145356565,"url":"https://api.github.com/repos/yiisoft/yii2/labels/status:need%20more%20info","name":"status:need more info","color":"d4c5f9","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-03-20T04:40:15Z","updated_at":"2017-03-21T03:49:59Z","closed_at":null,"body":"My business is \"Each issue need to be solved by one task. So if an issue doesn't have any task, we will create the new one then link to it\"\r\n\r\nIn Issue class, I declared relation \"task\" which store the task Id of this issue\r\n\r\n```php\r\nclass Issue extends ActiveRecord\r\n{\r\n        ...\r\n\tpublic function getTask()\r\n\t{\r\n\t\treturn $this->hasOne(Task::className(), ['id' => 'task_id']);\r\n\t}\r\n}\r\n```\r\n\r\nThe database tables:\r\n\r\nTable Issue:\r\n\r\n    - id\r\n    - name\r\n    - created_on\r\n    - created_by\r\n    - task_id\r\n\r\nTable Task:\r\n\r\n    - id\r\n    - name\r\n    - created_on\r\n    - status\r\n\r\n### What steps will reproduce the problem?\r\n\r\n```php\r\n$issue = Issue::findOne($issueId);\r\nif(!$issue->task_id) /*if this issue doesn't link any task*/\r\n{\r\n    $task = new Task();\r\n    $task->name = '[Issue] '.$issue->name;\r\n    ...\r\n    if($task->save()){\r\n        $issue->link('task', $task); /*link new task to this issue*/\r\n    }\r\n}\r\n...\r\n```\r\n\r\n### What is the expected result?\r\n\r\nThe task linked with issue via relation 'task'\r\n\r\n### What do you get instead?\r\n\r\n```\r\nFatal error: Call to a member function getIsNewRecord() on null \r\nIn ...\\vendor\\yiisoft\\yii2\\db\\BaseActiveRecord.php    at line 1233 \r\n```\r\n\r\nI debugged and found the model is null.\r\n![problem](https://cloud.githubusercontent.com/assets/10934695/24088767/28c3f954-0d61-11e7-8be5-27859243cec6.PNG)\r\n\r\n\r\n### Additional info\r\n\r\nThis error is only thrown for the first time. The second run (by refreshing the page) is fine because the task created at that time, so it can linked to issue successfully.\r\n\r\n| Q                | A\r\n| ---------------- | ---\r\n| Yii version      | 2.0.10\r\n| PHP version      | 5.6\r\n| Operating system | Windows 10\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/287969413","html_url":"https://github.com/yiisoft/yii2/issues/13805#issuecomment-287969413","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/13805","id":287969413,"user":{"login":"dkduyanh","id":10934695,"avatar_url":"https://avatars3.githubusercontent.com/u/10934695?v=3","gravatar_id":"","url":"https://api.github.com/users/dkduyanh","html_url":"https://github.com/dkduyanh","followers_url":"https://api.github.com/users/dkduyanh/followers","following_url":"https://api.github.com/users/dkduyanh/following{/other_user}","gists_url":"https://api.github.com/users/dkduyanh/gists{/gist_id}","starred_url":"https://api.github.com/users/dkduyanh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkduyanh/subscriptions","organizations_url":"https://api.github.com/users/dkduyanh/orgs","repos_url":"https://api.github.com/users/dkduyanh/repos","events_url":"https://api.github.com/users/dkduyanh/events{/privacy}","received_events_url":"https://api.github.com/users/dkduyanh/received_events","type":"User","site_admin":false},"created_at":"2017-03-21T03:49:59Z","updated_at":"2017-03-21T03:49:59Z","body":"Because each \"Issue\" may belongs to some categories, so I added inline-validation in the \"Issue\" model. The validation is \"REQUIRED\", this caused the Fatal error above, I'm sure because after being removed it works fine. \r\n\r\nTo solve this problem, I added new scenario and put the \"REQUIRED\" rule to it, and it works fine.\r\n\r\nMy code is:\r\n\r\n![issue](https://cloud.githubusercontent.com/assets/10934695/24131847/9e4c74f4-0e23-11e7-9095-ae020a3fc9a1.PNG)\r\n\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"dkduyanh"}}