Edward Posted December 21, 2013 Share Posted December 21, 2013 I just wanted to make a note here on my findings, i tested this in my active record: array('date_updated', 'default', 'value'=>new CDbExpression('NOW()'), 'on'=>'update'), This only worked for the first update of the record but if i updated the recorded once again it failed to work. The only way i could get a record update time to update succesfully every time was by doing this simple trick. public function beforeSave() { if($this->isNewRecord) { $this->date_created = new CDbExpression('NOW()'); } else { $this->date_updated = new CDbExpression('NOW()'); } return parent::beforeSave(); } Most of the times things work in Yii but there are the odd ocassions when they don't. But regardless of that I am very happy with it. If i am wrong about this and you know the way you can happily tell me how you did it below. Link to comment Share on other sites More sharing options...
Larry Posted December 27, 2013 Share Posted December 27, 2013 Thanks for sharing that. It surprises me that you have to do this, but bugs do exist. Thanks again! Link to comment Share on other sites More sharing options...
Edward Posted December 27, 2013 Author Share Posted December 27, 2013 Well there is still the possibly that i am wrong but i did test it and that was what i found. You can try testing this for yourself on your Yii Example Projects coming up on the Yii Book and see if you get the same results. May be its because i was on a local server however that seems to be unlikely. Oh i have an amazing picture to show you soon. By the way i saw you have many google plus accounts and was not sure which was yours as i wanted to add you to my circle, so what is happening there exactly? Link to comment Share on other sites More sharing options...
Larry Posted December 31, 2013 Share Posted December 31, 2013 Not sure about the many google plus accounts. Looks like it's under larryullman@gmail.com. Link to comment Share on other sites More sharing options...
Edward Posted July 29, 2014 Author Share Posted July 29, 2014 Funny stuff this! Today i can get the date_updated to update with array('date_updated','default','value'=>new CDbExpression('NOW()'),'on'=>'update'), God knows what i was doing before!! Link to comment Share on other sites More sharing options...
Edward Posted July 29, 2014 Author Share Posted July 29, 2014 Seriously sometimes i think i have a bad spirit going through my iMac, i mean one minute the code works then the next minute it doesn't, then it does again. I can't find any logical explanation for such errors. I am working a problem since yesterday i spent nearly 5 hours now and i am getting random results which is making it hard to pinpoint an exact error location. Link to comment Share on other sites More sharing options...
Edward Posted July 30, 2014 Author Share Posted July 30, 2014 Nah, array('date_updated','default','value'=>new CDbExpression('NOW()'),'on'=>'update'), This only works when you do the first update, but if you are to update a record again it will not work. So you are better of with: public function beforeSave() { if($this->isNewRecord) { $this->date_created = new CDbExpression('NOW()'); } else { $this->date_updated = new CDbExpression('NOW()'); } return parent::beforeSave(); } You could convert that to a tenary operator if you don't have other stuff you have to deal with. Just managed to figure out one particular bug it took me 7 hours. In the end it turned out to be nothing to do with my new code but something in another model that was failing to update. This activeRecord.saveAttributes() function pisses me off, it returns you a boolean whether the record was updated but if you are saving a record that is not modified it will return false. Very misleading and cost me a lot of time. Link to comment Share on other sites More sharing options...
Recommended Posts