Yii2更新查询产生的记录

时间:2017-07-07 22:27:55

标签: mysql select yii2

我需要使用从控制器传递的一些变量更新select查询产生的记录的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();
        }

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

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

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

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

 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();
    }