DAO Update, incremental value

Hello, yiiers!

Does anyone know if there is a way to make a query with DAO?




UPDATE my_table SET int_field = int_field + 1;



So I want to increment some field like this:




        (new Query())->createCommand()->update(

            MyTable::tableName(),

            ['int_field' => 'int_field +1']

        )->execute();



And this way is totally wrong because of value converting to integer.

Is raw sql the only option?

Try this:




(new Query())->createCommand()->update(

    MyTable::tableName(),

    ['int_field' => new Expression("int_field + 1")]

)->execute();



Use updateCounters() http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#updating-counters

Oh, right. AR method is helpful and exactly this but not what op wanted. Sorry.

Like this?


Yii::$app->db->createCommand('UPDATE my_table SET int_field = int_field+1')->execute();

If not, where are you trying to execute the sql and what is the purpose?

Thanks! It seems that’s the thing I’m looking for!