Yii framework how update all records?

2019-08-31 04:11发布

问题:

I am using Yii framework.

/view/update.php

<?php
    echo CHtml::beginForm();
    foreach ($items as $i=>$item):
        echo '<tr>';
    echo '<td>'.$item['name'].'</td>';
    echo '<td>'.$item['c1'].'</td>';
    echo '<td>'.  count(cos::model()->findAll(array(
            'condition'=>'cos2='.$item['id'],))).'</td>';
    echo '<td>'. CHtml::activeCheckBox($item, "[$i]c2").'</td>';
    echo '<td>'. CHtml::activeCheckBox($item, "[$i]c3").'</td>';
    echo '<td>'. CHtml::activeCheckBox($item, "[$i]c4").'</td>';
    echo '<td>edit</td>';
        echo '</tr>';
    endforeach;
    echo '</table>';
    echo CHtml::submitButton('Save');
    echo CHtml::endForm();
?>

how can I write all the records at once?

回答1:

Use updateAll

As an example

User::model()->updateAll(array( 'status' => 1, 'updated' => date('Y-m-d' ),
                              'type_id = 1 AND status = 0 '
                      );


回答2:

you can use this as reference on how to use the Yii update all

CActiveRecord::updateAll($attributes, $condition='', $params=array())

http://www.saidur-rahman.com/yii-how-to-updateall-works/



标签: php mysql yii