Jozsef Naghi
Jozsef Naghi

Reputation: 1095

How do I update a row in Yii without using the primary key?

This is my code:

$model= Prices::model()->findByPk($pk);
$model->status = 1;
$model->update(array('status'));

This update my row using the primary key, but how do I update a query without the pk in Yii. For instance:

How do I write this query:

update mytable set mycolumn = '1' where mycolumn1 ='2' and mycolumn2 = '3' and mycolumn3 = '4' ? Thx

Upvotes: 1

Views: 731

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Try this way to update your model using UpdateAll()

Prices::model()->updateAll(
 array('mycolumn'=>1),'mycolumn1=:mycolumn1 AND
 mycolumn2=:mycolumn2 AND mycolumn3=:mycolumn3',
 array(':mycolumn1'=>2,':mycolumn1'=>3,':mycolumn1'=>4)
);

Upvotes: 0

Nemutaisama
Nemutaisama

Reputation: 611

Did you tried first string like this?

$model= Prices::model()->findByAttributes(
    array(
        'mycolumn1' => 2,
        'mycolumn2' => 3,
        'mycolumn3' => 4
     )
);
$model->mycolumn = 1;
$model->update(array('mycolumn'));

Upvotes: 1

Related Questions