[SLOVED] 关于手工操作数据表问题~


$sql = 'select * from {{tree2}} where sc_content != ""';

$cmd = $conn->createCommand($sql);

$rows = $cmd->queryAll();


foreach($rows as $row) {

    $updateSql = 'update {{tree}} set sc_content = :sc_content where cid=:cid';

    $cmd = $conn->createCommand($updateSql);

    $cmd->bindParam('sc_content', $row['sc_content']);

    $cmd->bindParam('cid', $row['cid']);

    $cmd->execute();

}

类似于上面的更新数据表的做法,使用了$conn->createCommand($sql)方法,

这个方法是会创建一个新的 CDbCommand 实例,假如我上面的 $rows 有1w条,

那么上面的操作就建立了1w个cmd实例,请问是否可以简化? 提高效率~

update {{tree}} set sc_content=b.sc_content from {{tree}} a inner join {{tree2}} b on a.cid=b.cid

一条sql搞定

谢谢!

但我提的这个问题,关键不在具体的语句,而是,是否可以避免实例化多个 CDbCommand 实例的问题~

你可以这样子:




$sql = 'select * from {{tree2}} where sc_content != ""';

$cmd = $conn->createCommand($sql);

$rows = $cmd->queryAll();


$updateSql = 'update {{tree}} set sc_content = :sc_content where cid=:cid';

$cmd = $conn->createCommand($updateSql);

$cmd->bindParam('sc_content', $content);

$cmd->bindParam('cid', $cid);


foreach($rows as $row) {

    $content = $row['sc_content'];

    $cid = $row['cid'];

    $cmd->execute();

}



确实是个good idea~

创建一个对象属性实例,改变其属性。 面向对象的基本怎么能忘记了,不用实例那么对象。

好!