【问题标题】:Yii2 Update records resulting from queryYii2 更新查询产生的记录
【发布时间】:2017-07-09 03:23:44
【问题描述】:

我需要使用从控制器传递的一些变量更新由选择查询产生的记录的 month_no 字段,但每个字段都写入相同的值。

这些是变量:

    $id_calc = 27
    $from_y = "2013-05-01"
    $to_y = "2015-11-31"

表格结果:

   id   id_paid   year_ref     month_no
    1        26      2012         12
    2        26      2013         12
    4        27      2013         12  (should be 8)
    5        27      2014         12
    6        27      2015         12  (should be 11)

这是模型:

 public function CorrectTable($id_calc, $from_y, $to_y)
    {  
        $connection = Yii::$app->db;
        $query = "SELECT * FROM my_table where id_paid='$id_calc'";
        $data = $connection->createCommand($query)->queryAll();
        // calculate no. of month first and last year
        $month_no_begin = (13 - date("m",strtotime($from_y)));   
        $month_no_end = (date("m",strtotime($to_y)));
        // 
        foreach($data as $row) 
        {
          $id = $row['id'];
          // calculate number of month per year
          if ($row['year_ref'] = date("Y",strtotime($from_y))) 
          {
            $month_no = $month_no_begin;
          } elseif  ($row['year_ref'] = date("Y",strtotime($to_y))) 
          {
            $month_no = $month_no_end;
          } else
          {
            $month_no = 12;
          }
            Yii::$app->db->createCommand()
            ->update('my_table', ['month_no' => $month_no], ['id' => $id])
            ->execute();
        }

我尝试将上面的代码放在控制器中,结果相同。

【问题讨论】:

    标签: mysql select yii2


    【解决方案1】:

    我曾经犯过一个粗心的错误。您应该在if 语句中使用===== 来测试相等条件,而不是=

    public function CorrectTable($id_calc, $from_y, $to_y)
    {  
        $connection = Yii::$app->db;
        $query = "SELECT * FROM my_table where id_paid='$id_calc'";
        $data = $connection->createCommand($query)->queryAll();
        // calculate no. of month first and last year
        $month_no_begin = (13 - date("m",strtotime($from_y)));   
        $month_no_end = (date("m",strtotime($to_y)));
        // 
        foreach($data as $row) 
        {
            $id = $row['id'];
    
            // use `==` instead of `=`
            if ($row['year_ref'] == date("Y",strtotime($from_y))) 
            {
                $month_no = $month_no_begin;
            }
    
            // use `==` instead of `=`
            elseif ($row['year_ref'] == date("Y",strtotime($to_y))) 
            {
                $month_no = $month_no_end;
            }
            else
            {
                $month_no = 12;
            }
    
            Yii::$app->db->createCommand()
                ->update('my_table', ['month_no' => $month_no], ['id' => $id])
                ->execute();
        }
    }
    

    【讨论】:

    • 这个答案是错误的,不会导致 OP 问题
    • @scaisEdge 你不得不承认这个OP的问题是由于使用了错误的操作符造成的。我认为 OP 在条件逻辑中不会有任何问题。但你是对的,我应该删除我的答案还是应该更改我的答案?
    • 你回答解决了在 if 中使用 fo = / == 的问题,但不解决 OP 的逻辑 .. 是一个很好的建议,但也不是正确的答案
    • @scaisEdge 你能告诉我我哪里错了吗?
    • @scaisEdge 谢谢!坦率地说,我喜欢你处理事情的风格。
    【解决方案2】:

    我们的 if ... esleif ... else 序列似乎不是 logig 正如你所做的那样,else if 永远不会被执行

    所以你应该以这种方式改变条件

     foreach($data as $row) 
        {
            $id = $row['id'];
    
            // assigne default value
            $month_no = 12;
    
            // check for month_begin
            if ($row['year_ref'] == date("Y",strtotime($from_y))) 
            {
                $month_no = $month_no_begin;
            }
    
            //check for month_end    
            if ($row['year_ref'] == date("Y",strtotime($to_y))) 
            {
                $month_no = $month_no_end;
            }
    
    
            Yii::$app->db->createCommand()
                ->update('my_table', ['month_no' => $month_no], ['id' => $id])
                ->execute();
        }
    

    【讨论】:

    • 谢谢@scaisEdge。它可以工作,代码更干净
    • 你确实解决了 OP 的问题。为了匹配 OP 的结果,你完全改变了风格。顺便说一句,你代码中的 if 语句会不断地执行两次,$month_no = $month; 也可能是。与 OP 相比,您的回答实际上带来了额外的开销。
    • @gdel ...我很抱歉,但正确的答案是保罗的答案..所以你不应该将我的答案标记为已接受,而是将保罗标记为..(我之前的评论是错了..
    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    相关资源
    最近更新 更多