afters在yii中不起作用

时间:2014-01-15 11:59:04

标签: date datetime yii

aftersave在yii中不起作用.......在这里我继续在tbl1中创建记录我需要更新tbl1中的到期日期即。通过添加tbl2持续时间来完成以下是代码

parent::afterSave();
    if ($this->isNewRecord)
    {
        $sql1="select sid,createdate 
        from tbl1
        where(pid=". $_GET['ppid'] ." and mpid=". $this->mid. ")";
        $edata=Yii::app()->db->createCommand($sql1)->queryRow();


        var_dump($edata);
        $sql2="select duration
        from tbl2
        where ( pid=". $_GET['ppid'] ." and sid=".$edata['sid'].")";

$duration=Yii::app()->db->createCommand($sql2)->queryScalar();

        var_dump($duration);
    //  $expirydate=date('Y-m-d H:i:s', strtotime($edate['createdate']."+ $duration"));
    $expirydate = date("Y-m-d H:i:s", strtotime($edata['createdate'] . " + $duration "));
        var_dump($expirydate);
        $sql="update tbl1 set expirydate=".$expirydate." where ( mpid=" .$this->mid ." and pid=".      $_GET['ppid'].")";
             $connection=Yii::app()->db;
                $command=$connection->createCommand($sql);
                      $tts=$command->execute();

我得到的错误是

  CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00 where ( mid=42 and prid=4)' at line 1. The SQL statement executed was: update tbl1 set expirydate=1970-01-01 00:00:00 where ( mid=42 and pid=4) 

1 个答案:

答案 0 :(得分:1)

试试这个

"update tbl1 set expirydate='".$expirydate."' where 

您需要将有效期限保留在单引号内

注意: - 我在'之前和".$expirydate之后添加了$expirydate."

更新

尝试以下更改

$expirydate = date("Y-m-d H:i:s", strtotime("$edata['createdate'] + $duration "));