dongpan2788 2019-01-15 21:37
浏览 73

无法将下拉字符串值与yii2中的int(用户角色)进行比较

Im trying to create an 'apoderado' or a 'profesor' in it's respective tables while I create an user in table 'users' depending on dropdown selected value. If it's 'apoderado' create the user and an apoderado, else creates an user and a profesor. But my code is doing nothing but create the user.

I tried with arrays and method in_array with no success as I said in Yii forum https://forum.yiiframework.com/t/dropdown-value-with-array-data-wont-work-in-if-condition-user-role/124633

SiteController.php

public function actionRegister()
    {
        $model = new FormRegister;
        $msg = null;
        if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax)
        {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(Yii::$app->request->post()))
        {
            if($model->validate())
            {
                $table = new Users;
                $table->role = $model->role;
                $table->username = $model->username;
                $table->password = crypt($model->password, Yii::$app->params["salt"]);
                $table->authKey = $this->randKey("abcdef0123456789", 200);
                $table->accessToken = $this->randKey("abcdef0123456789", 200);
                $table->email = $model->email; 
//                var_dump($model->role); die;
                if ($model->role == '1'){
                    $apoderado = new Apoderado;
                    $apoderado->nombre = $model->nombre;
                    $apoderado->rut = $model->rut;
                    $apoderado->fono = $model->fono;
                    $apoderado->direccion = $model->direccion;
                    $apoderado->email = $model->email;
                    $apoderado->id_alumno = $model->id_alumno;
                    $apoderado->apoderado_sup = $model->apoderado_sup;
                    $apoderado->fono_apoderado_sup = $model->fono_apoderado_sup;
                    $apoderado->email_apoderado_sup = $model->email_apoderado_sup;
                    $apoderado->save();
                } else {
                    $profesor = new Profesor;
                    $profesor->nombre = $model->nombre;
                    $profesor->rut = $model->rut;
                    $profesor->fono = $model->fono;
                    $profesor->direccion = $model->direccion;
                    $profesor->email = $model->email;
                    $profesor->save();
                } 
//                else {
//                // check what's happening
//                var_dump($model->role); die;
//                }

                $table->id_apoderado = $model->id_apoderado;
                $table->id_profesor = $model->id_profesor;

                //Si el registro es guardado correctamente
                if ($table->insert())
                {
                    $user = $table->find()->where(["email" => $model->email])->one();
                    $id = urlencode($user->id);
                    $authKey = urlencode($user->authKey);
                    $subject = "Confirmar registro";
                    $body = "<h1>Haga click en el siguiente enlace para finalizar tu registro</h1>";
                    $body .= "<a href='http://localhost:8080/sie2gii/web/index.php?r=site/confirm&id=".$id."&authKey=".$authKey."'>Confirmar</a>";
                    Yii::$app->mailer->compose()
                        ->setTo($user->email)
                        ->setFrom([Yii::$app->params["adminEmail"] => Yii::$app->params["title"]])
                        ->setSubject($subject)
                        ->setHtmlBody($body)
                        ->send();
                    $model->nombre = null;
                    $model->rut = null;
                    $model->fono = null;
                    $model->direccion = null;
                    $model->email = null;
                    $model->username = null;
                    $model->password = null;
                    $model->password_repeat = null;
                    $msg = "Enhorabuena, ahora sólo falta que confirmes tu registro en tu cuenta de correo";
                }
                else
                {
                    $msg = "Ha ocurrido un error al llevar a cabo tu registro";
                }
            }
            else
            {
                $model->getErrors();
            }
        }
        return $this->render("register", ["model" => $model, "msg" => $msg,]);


Register.php (view)

<div class="form-group">
    <?= $form->field($model, 'role')->dropDownList(
            ['1' => 'Apoderado', '2' => 'Profesor'], 
            ['prompt' => 'Seleccione tipo de usuario']); ?>
</div>

I need to do this:

if ($model->role == '1'){
                    $apoderado = new Apoderado;
                    $apoderado->nombre = $model->nombre;
                    $apoderado->rut = $model->rut;
                    $apoderado->fono = $model->fono;
                    $apoderado->direccion = $model->direccion;
                    $apoderado->email = $model->email;
                    $apoderado->id_alumno = $model->id_alumno;
                    $apoderado->apoderado_sup = $model->apoderado_sup;
                    $apoderado->fono_apoderado_sup = $model->fono_apoderado_sup;
                    $apoderado->email_apoderado_sup = $model->email_apoderado_sup;
                    $apoderado->save();
                } else {
                    $profesor = new Profesor;
                    $profesor->nombre = $model->nombre;
                    $profesor->rut = $model->rut;
                    $profesor->fono = $model->fono;
                    $profesor->direccion = $model->direccion;
                    $profesor->email = $model->email;
                    $profesor->save();
                }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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