Search code examples
activerecordyiicriteria

update without select active record yii


I try update num_of_gifts without select use yii active record and CDbCriteria this is my code:

$attributes = array('num_of_gifts' => 'num_of_gifts'+1);
$condition = 'bla bla';
$params = array(bla bla);
Gifts::model()->updateAll($attributes, $condition, $params);

all I get that "1" at the num_of_gifts cell, is it possible increase 1 to num_of_gifts? or I need select the amount of num_of_gifts and after it use update?


Solution

  • If you are just updating counters you could use Yii's updateCounters method.

    Example for updating all:

    Gifts::model()->updateCounters(array('num_of_gifts'=>1));
    

    That method takes a condition to update a specific record, but if you don't include a condition it will update all records.

    [edit]

    If you want to update selected records based on a condition, the format is this:

    Gifts::model()->updateCounters(
            array('num_of_gifts'=>1),
            array('condition' => "param = :param"),
            array(':param' => 'paramvalue'),
        );