dongyingjiu0669 2011-06-11 23:42
浏览 23
已采纳

Doctrine查询更新用户表

I have a form with the following fields:

  • email
  • password
  • url
  • responsable
  • pais (this is a multicheckbox)
  • role

And I've created this method within the Model_Users class

public function updateUser($user) {
    $q = Doctrine_Query::create()
        ->update('Model_Users usr'); //aca estaba el ;

    foreach ($user as $k => $v) {
        if ($k == 'pais' || $k == 'id') {
            continue;
        } else {
            $q->set("usr.$k", '?', $v);
            //echo "Key: $k => Value: $v ||";
        }
    }

    $q->where("usr.id=$user[id]");
    $q->execute();
}

The problem is that this isn't really updating... and if I do something like echoing $q->getSqlQuery() the SQL query I get is UPDATE users SET email = ?, password = ?, url = ?, responsable = ?, role = ? WHERE (id = 150).

I'm guessing I cannot use a foreach loop to get this right,... so what would be the way around it?

  • 写回答

1条回答 默认 最新

  • duanjuda5789 2011-06-12 00:13
    关注

    If you already have a Model_User instance available then it should be for the record you are trying to update. Otherwise you just use a query. The updateUser doesnt belong on Model_User it belongs on Model_User_Table assuming you are actually generating the Doctrine table classes for each model (which i personally recommend).

    To answer your actual question though it shoudl look something like this:

       $q = Doctrine_Query::create()
            ->update('Model_Users');
    
       foreach ($user as $k => $v) {
            if ($k == 'pais' || $k == 'id') {
                continue;
            } else {
                $q->set($k, $v);
            }
        }
    
        $q->where("usr.id = ?", $user[id]);
        $q->execute();
    

    As for setting pais that code is goign to depend on how pais is stored in the DB (what doctrine type is it - array, object, string, M2m relation?).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口