{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/7791","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/7791/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/7791/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/7791/events","html_url":"https://github.com/yiisoft/yii2/issues/7791","id":63027463,"number":7791,"title":"TimestampBehavior problem when using expression","user":{"login":"wartur","id":1358236,"avatar_url":"https://avatars.githubusercontent.com/u/1358236?v=3","gravatar_id":"","url":"https://api.github.com/users/wartur","html_url":"https://github.com/wartur","followers_url":"https://api.github.com/users/wartur/followers","following_url":"https://api.github.com/users/wartur/following{/other_user}","gists_url":"https://api.github.com/users/wartur/gists{/gist_id}","starred_url":"https://api.github.com/users/wartur/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wartur/subscriptions","organizations_url":"https://api.github.com/users/wartur/orgs","repos_url":"https://api.github.com/users/wartur/repos","events_url":"https://api.github.com/users/wartur/events{/privacy}","received_events_url":"https://api.github.com/users/wartur/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-03-19T16:15:49Z","updated_at":"2015-03-20T08:22:39Z","closed_at":null,"body":"Hello.\r\n\r\nThis is problem: \"NOW()\"\r\n\r\n![screenshot from 2015-03-19 19 08 06](https://cloud.githubusercontent.com/assets/1358236/6734823/61a126c0-ce6b-11e4-8d1c-7713feff4aee.png)\r\nI think that the correct conclusions empty field. Or old value.\r\n\r\n### To Reproduce\r\n```php\r\n<?php\r\n\r\nnamespace app\\models\\activeOverride;\r\n\r\nuse Yii;\r\nuse yii\\behaviors\\TimestampBehavior;\r\n\r\nclass Agency extends \\app\\models\\activeRecords\\Agency\r\n{\r\n    /**\r\n     * @inheritdoc\r\n     */\r\n    public function rules()\r\n    {\r\n        return [\r\n            [['id', 'active'], 'integer'],\r\n            [['created_at', 'updated_at'], 'safe'],\r\n        ];\r\n    }\r\n    /**\r\n     * @inheritdoc\r\n     */\r\n    public function behaviors()\r\n    {\r\n        return array_merge(parent::behaviors(), [\r\n            TimestampBehavior::className() => [\r\n                'class' => TimestampBehavior::className(),\r\n                'value' => new \\yii\\db\\Expression('NOW()')\r\n            ],\r\n        ]);\r\n    }\r\n    public function beforeSave($insert)\r\n    {\r\n        if(parent::beforeSave($insert)) {\r\n            // update some relation models\r\n            $this->addError('active', 'error update relations');\r\n            return false;  // I think this is the right work with the framework. Right?\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nUPDATE: add my rules and comment"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/83955509","html_url":"https://github.com/yiisoft/yii2/issues/7791#issuecomment-83955509","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/7791","id":83955509,"user":{"login":"mikehaertl","id":675062,"avatar_url":"https://avatars.githubusercontent.com/u/675062?v=3","gravatar_id":"","url":"https://api.github.com/users/mikehaertl","html_url":"https://github.com/mikehaertl","followers_url":"https://api.github.com/users/mikehaertl/followers","following_url":"https://api.github.com/users/mikehaertl/following{/other_user}","gists_url":"https://api.github.com/users/mikehaertl/gists{/gist_id}","starred_url":"https://api.github.com/users/mikehaertl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikehaertl/subscriptions","organizations_url":"https://api.github.com/users/mikehaertl/orgs","repos_url":"https://api.github.com/users/mikehaertl/repos","events_url":"https://api.github.com/users/mikehaertl/events{/privacy}","received_events_url":"https://api.github.com/users/mikehaertl/received_events","type":"User","site_admin":false},"created_at":"2015-03-20T08:22:39Z","updated_at":"2015-03-20T08:22:39Z","body":"@wartur You should not do it because you get into exactly the trouble you are now. Behaviors like the TimestampBehavior expec, that on the beforeSave event, we are almost writing to DB now and can do last-minute changes to the record. Yes, they could stop the save, but that should not be because of validation errors at this point.\r\n\r\n> PS: afterValidate can't stop save process.\r\n\r\nOf course you can. Just add an error.\r\n\r\nAnd there's no reason why you could not replace beforeSave with afterValidate. You can use `$this->isNewRecord` to find out, if it's an insert.\r\n"}}