2018-11-07 14:54:42 3546次浏览 4条回答 0 悬赏 25 金钱

$sql->createCommand()->update('table',$arr,'uid= :name',[':name'=>$id])->execute();

实际生成

UPDATE table SET state=1 WHERE uid= '1000020'

想实现

UPDATE table SET state=1 WHERE uid= '1000020' and time=20181107

createCommand()->update 该怎么写。看过手册了,没有具体的范例,

  • 回答于 2018-11-07 17:27 举报

    你可以看看这篇文章:https://www.cnblogs.com/liangzia/p/5937378.html 希望有所帮助!!!

    1 条回复
    回复于 2018-11-07 20:06 回复

    多谢大佬!

  • 回答于 2018-11-07 17:33 举报
    /**
    * @param string $table the table to be updated.
    * @param array $columns the column data (name => value) to be updated.
    * @param string|array $condition the condition that will be put in the WHERE part. Please
    * refer to [[Query::where()]] on how to specify condition.
    * @param array $params the parameters to be bound to the command
    * @return $this the command object itself
    */
    public function update($table, $columns, $condition = '', $params = [])
    {
    $sql = $this->db->getQueryBuilder()->update($table, $columns, $condition, $params);
    
    return $this->setSql($sql)->bindValues($params);
    }
    

    condition : @param string|array $condition the condition that will be put in the WHERE part. Please

    1 条回复
    回复于 2018-11-07 20:04 回复

    小学生看不懂e文

  • 回答于 2018-11-07 19:47 举报

    这个简单的很,一看就是一个懒家伙,文档说得那么详细了都不看一遍。至少应该在【数据库访问】部分看一遍再提问吧?
    https://www.yiichina.com/doc/api/2.0/yii-db-command#update()-detail

    另外还有一个更快捷的办法,在AR(ActiveRecord)里。如下:
    public static function updateAll($attributes, $condition = '', $params = [])
    {
        $command = static::getDb()->createCommand();
        $command->update(static::tableName(), $attributes, $condition, $params);
    
        return $command->execute();
    }
    其中第二个参数$condition可以是字符串,也可以是关联数组。你的and条件用关联数组就行。
    

    小伙子以后花几分钟看看所有文档有关数据库部分,你会发现好多。

    5 条回复
    回复于 2018-11-07 20:05 回复

    谢谢大叔@刘师傅

    $minAge = 30;
    createCommand()->update('user',$arr, 'age > :minAge', [':minAge' => $minAge])->execute();
    
    createCommand()->update('table',$arr,'uid= :name',[':name'=>$id])->execute();
    

    好像还是一样的

    这样不行把
    $aaa=['and', 'id=1', 'id=2'] ;
    update('table',$arr,$aaa)

    回复于 2018-11-08 09:42 回复

    哎,真是的,这么不灵活呢,我有点生气了啊!看过文档你就不会这么写了。
    update('table',['f1'=>v1,'f2'=>v2],['id1=>1,'id2'=>2]);就行。条件['id1=>1,'id2'=>2]这样写。

    回复于 2018-11-08 10:24 回复

    晕哦,不是吧,条件直接用数组啊,我记得我当时把前面中括号括起来 ,直接报错了⊙︿⊙

    回复于 2018-11-08 10:28 回复

    哈哈!~我有点生气了啊!

    回复于 2018-11-08 12:16 回复

    摸了半天,原来和select的where一毛样的用法,

                    [
                    'and',
                    'uid=:id',
                    'num=:time'
                        ], [
                            ':id' => $id,
                            ':time' => $time
                        ]```
    
  • 回答于 2018-11-08 09:09 举报

    $sql->createCommand()->update('table',$arr,['uid'=>' :name','time'=>':time'],[':name'=>$id,':time'=>$time])->execute();
    难道不应该是这样吗?

    1 条回复
    回复于 2018-11-08 10:26 回复

    估计是层主你那样的。

    当时我把
    update('table',$arr,[':name'=>$id]),好像是报错了的

您需要登录后才可以回答。登录 | 立即注册
开门_查水表
助理

开门_查水表

注册时间:2018-09-28
最后登录:2018-11-14
在线时长:6小时2分
  • 粉丝0
  • 金钱115
  • 威望0
  • 积分175

热门问题