简体   繁体   English

Yii 框架如何更新所有记录?

[英]Yii framework how update all records?

I am using Yii framework.我正在使用 Yii 框架。

/view/update.php /查看/更新.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?我怎样才能一次写下所有记录?

Use updateAll使用更新所有

As an example举个例子

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

you can use this as reference on how to use the Yii update all您可以将其用作有关如何使用 Yii update all 的参考

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

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

In Yii2, if you are using a Bind Variable (which is recommended) then use the below syntax.在 Yii2 中,如果您使用绑定变量(推荐),请使用以下语法。 In the eg Sometable's tr_id and completeby_dt are updated to Null where column tr_id = variable $tr_id.例如,Sometable 的 tr_id 和 completeby_dt 更新为 Null,其中列 tr_id = 变量 $tr_id。

Sometable::updateAll(['tr_id' => NULL, 'completeby_dt' => NULL],
                     'tr_id = :vtrid', [':vtrid' => $tr_id]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM