{"action":"closed","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9419","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9419/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9419/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9419/events","html_url":"https://github.com/yiisoft/yii2/issues/9419","id":101058676,"number":9419,"title":"ActiveRecord updateCounters fails when target field is Null","user":{"login":"dbd5","id":9496750,"avatar_url":"https://avatars.githubusercontent.com/u/9496750?v=3","gravatar_id":"","url":"https://api.github.com/users/dbd5","html_url":"https://github.com/dbd5","followers_url":"https://api.github.com/users/dbd5/followers","following_url":"https://api.github.com/users/dbd5/following{/other_user}","gists_url":"https://api.github.com/users/dbd5/gists{/gist_id}","starred_url":"https://api.github.com/users/dbd5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dbd5/subscriptions","organizations_url":"https://api.github.com/users/dbd5/orgs","repos_url":"https://api.github.com/users/dbd5/repos","events_url":"https://api.github.com/users/dbd5/events{/privacy}","received_events_url":"https://api.github.com/users/dbd5/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:docs","name":"type:docs","color":"207de5"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-14T17:23:36Z","updated_at":"2015-09-28T20:44:38Z","closed_at":"2015-09-28T20:44:38Z","body":"Hello,\r\n\r\nIt seems that updateCounters fails when the target field is null.\r\n\r\nTo reproduce, create a counter field like 'view_counter' in any table, then try to update field when form is viewed with code like this;\r\n\r\npublic function actionView($id)\r\n{\r\n    $model = $this->findModel($id);\r\n    $model->updateCounters(['view_count' => 1]);\r\n\r\n    return $this->render('view', [\r\n        'model' => $model,\r\n    ]);\r\n}\r\n\r\nNo update occurs and updateCounters returns false.\r\n\r\nMy theory is that Yii attempts UPDATE post SET view_count = view_count + 1 WHERE id = 100 which evaluates as SET view_count = Null + 1 WHERE id = 100 thus failing\r\n\r\nNot sure if this requires a fix or clearer error message but maybe updating AR documentation with a note suggesting initializing counter fields to zero before any call to updateCounters will suffice?\r\n\r\nRegards,\r\n\r\nAdam"}}