Yii - Updating a MySQL datetime column with now()

Just a quick tip which took me a little while to figure out. If you have a "last_updated" column on your table and you want to update this manually in your code ten the easiest way would be use the MySQL NOW() function Update some_table set last_updated_date=now(); However, and there is always a catch, if you try and use the now() function in a Yii prepared statement, it will try and update the column with the string "now()" and fail. What you need to do is to use a CDBExpression object as follows:- $command->insert('some_table', array( 'id'=>$id, /.. other columns 'last_updated_dt'=>new CDbExpression('NOW()') ));

Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com