What's Wrong With Cdbexpression?

I am trying to use cDbExpression like this (the most common use):

echo new CDbExpression(‘now()’);

all I get returned from this method is the expression string (no matter what string)

for example : new CDbExpression(‘blah’); returns blah

What am I doing wrong?

CDbExpression itself won’t connect to database and won’t fetch any data. Its role is only to ensure that the expression it holds will not be escaped in the generated SQL.

You can find some information about its usage here.

EDIT: link works now

Could you give a usage example?

I just can’t figure this one out :frowning:

The line I am actually using right now is:

$model->up_Date = new CDbExpression(‘NOW()’);

Where the model specifies up_Date as

(in model file):

  • @property string $up_Date

and in the mySQL database up_Date is of the type datetime



Seems to be correct so far. Now try


$model->refresh(); // re-populates $model with current data from database

echo $model->up_Date; // should display a datetime


this works, however it doesn’t seem to be a great solution since it involves saving and refreshing and I think this will trigger validation, which I don’t want at this stage, This happens when a form is loaded.

I found that I am more confident with just pulling the current date out of the database server using

$command=Yii::app()->db->createCommand("Select now();")->queryAll();

$this->up_Date= $command[0][‘now()’];