UpdateAll example

Hi all,

I am a Yii newbie. The functionality that I am trying to accomplish is upon creation of a new record, I would like to set all of the “current” columns to 0 with records that have the same parentId. I think that this type of code belongs in beforeSave but I haven’t seen any examples of updateAll. Would my attributes be just the current column and since I would be doing this call from the model layer would I be using $this or would I need to create a new instance of the class?

Thanks.

For a start, I found this in the CHM version of the class reference (not included online)

online prototype

My guess:




YourModel::model()->updateAll(array('current'=>0),'parentId="'.$this->parentId.'"',);



/Tommy

Will Try. Thanks Tommy

Thanks so much Tommy. That worked. I ended up with the following

YourModel::model()->updateAll(array(‘current’=>0),‘parentId="’.$this->parentId.’"’);

stripped off the trailing comma. Thanks.

How can I do it yii using updateAll or …




update tbl_category set priority = priority+1 where priority>='.$this->priority



Please fill the question mark




protected function beforeSave()

{

	if(parent::beforeSave())

	{

		$model1 = Category::model()->updateAll(array('priority'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />??),'priority>=:priority',array(':priority'=>$this->priority));

		return true;

	}

	else

	{

		return false;

	}

}



Please help me

I got it. Will use


Category::model()->updateCounters(array('priority'=>1),'priority>=:priority',array(':priority'=>$this->priority));

I need one more help

I have data like this




id	name	priority

1	cat2	2

2	cat3	3

3	cat1	1

After I delete id=3 and then insert a new item with the priority=1




id	name	priority

1	cat2	3

2	cat3	4

4	new cat	1

the same one order by priority




id	name	priority

4	new cat	1

1	cat2	3

2	cat3	4



How can I re-arrange the priority as 1,2,3 like this

Please help me