duanhuanzhi6431 2014-09-18 22:34
浏览 18
已采纳

yii框架,其中方法未正确保存

I am using Yii framework and I am creating a register page for the user. In order for a user to create an account, they need to enter in a ticket code. A widow form pops up and they fill out the ticket code, it ajax validates it properly and stores it as a session variable. The popup window closes and allows the user to fill out the rest of the form and submit it. The form should then validate the information, create the user, reasign the ticket's user_ID to the new user_ID, and load the page /ticket/mytickets.

what happens, the ticket is confirmed that it exists, saves it into the session, the user is created, the ticket reassign method gets called, the ticket is never reassigned, and the page reloads. When I echo out the page on the reload, it shows the correct information for the user_ID, and the ticket_ID.

Any hints would help for debugging this would be helpful. Thank you

//controler
public function actionRegister()
{

    $model=new User;
    $valid = false;
    //check to see if valid
    if(isset($_POST['User'])){
        $valid = $model->checkValid();
    }
    if($valid)
    {
        $model->attributes=$_POST['User'];
        $user_ID = $model->ID;
        if($model->save()){
            //save ticket to user
            $reassigned = Ticket::model()->reassign_with_IDs($_SESSION['ticket_ID'],$user_ID);
            if($reassigned){
                unset($_SESSION['ticket_ID']);
                //redirect to mytickets
                $this->redirect(array('/ticket/mytickets'));
            }
        }
    }

    $this->render('register',array(
        'model'=>$model,
    ));

}


//model
public static function reassign_with_IDs($ticket_ID,$user_ID){
    $ticket = Ticket::model()->findByPK($ticket_ID);
    $ticket->user_ID = $user_ID;
    if($ticket->save()){
        return true;
    }
    else{
        return false;
    }
}
  • 写回答

1条回答 默认 最新

  • duanqin4238 2014-09-19 02:46
    关注

    $model->ID is only set after $model is saved. Therefore your code should read:

    if($valid)
    {
        $model->attributes=$_POST['User'];
        if($model->save()){
            //save ticket to user
            $user_ID = $model->ID;
            $reassigned = Ticket::model()->reassign_with_IDs($_SESSION['ticket_ID'], $user_ID);
    

    You can however get rid of the reassign_with_IDs function and the unnecessary variable user_ID and just explicitly set the ticket using:

    $reassigned = Ticket::model()->updateByPk($_SESSION['ticket_ID'], ['user_ID'=>$model->ID]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值