Using CDbCriteria to generate update queries with computed column value

Hello Yii Developers,

I use Yii for some months now and really like the framework.

Now I have come across a problem I am not able to solve by myself (or at least not quickly without debugging into the depths on the framework) or find on the internet.

The problem is the following:

I’m trying to do composite (not sure about it’s official naming) SQL update statement like:

update tab set field1 = field2 + field3 where fieldX = 123

To generate this statement I use the CDbCriteria object and initi it the following way:

$criteria = new CDbCriteria();

$criteria->condition = ‘fieldX = 123’;

$attributes = array(

‘field1’=>‘field2 - field3’


self::model()->updateAll($attributes, $criteria);

The error I get is the following (yes, it’s MSSQL)

[Microsoft][SQL Server Native Client 10.0][SQL Server]Error converting data type nvarchar to float…

As I see , the problem is, that Yii tries to prepare the statement and format the computation based on other columns as a value.

I hope anybody can help.

Thanks in advance,



$attributes = array(

  'field1' => new CDbExpression('field2 - field3'),


Yes! That’s exactly the thing I was looking for. Works now, thank you very much!!