Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii migration update

Tags:

php

migration

yii

In a migration, I want to add an order column which defaults to the ID of the column. I tried the following:

$this->update(
 'item',  // table
  array(  // columns
    'item_order'=>':item_id'
  ), 
  '',     // condition
  array(  // parameters
    ':item_id'=>'item_id'
  )
);

But this just gives everything ID 0. (I'm not really surprised, since I'm guessing it's trying to use the string as opposed to the column name).

Any way to get this done without manually constructing SQL?

like image 738
Michael Mior Avatar asked Mar 25 '11 17:03

Michael Mior


1 Answers

Wrap the column name in a CDbExpression, which instructs Yii to include it in the resulting query unescaped:

$this->update('item', array('item_order'=> new CDbExpression('item_id')));
like image 132
Jon Avatar answered Oct 16 '22 12:10

Jon