duanchun2349 2016-11-22 20:46
浏览 40
已采纳

为Yii2创建multi_models注册页面并在会话中保存该信息

I am working on a personal Yii2 project and I am stuck! I don't know how to :

  • create a sing up page which has a form to create two related models (Organization & Employee ) and use a third one (Employee_Role)
  • Store these info in a session and use that session later.

General Scenario :

Admin signups by filling : an Organization name and username & password & email (for the Admin employee)

if the values are valid then the system creates "Organization" with an auto_generated organization_id.

Then, System creates "Employee" (which will need organization_id and assign this user to Admin "Employee_Role")

Then, the system keeps the following info in session: (organization_id, employee_id, role_id, timestamp ) and takes the user to admin home page.

Note, I am keeping the Models in the Common folder and the Controllers in the front-end and so should be the Views.

I appreciate your help,

  • 写回答

1条回答 默认 最新

  • dongshen7407 2016-11-23 18:27
    关注

    This is a general outline of what you can do to solve your problem. You will need to add the actual fields to the form for both Organization and Employee.

    The controller action:

    public function actionSignup() {
        $post = Yii::$app->request->post();
    
        $organization = new Organization();
        $employee = new Employee();
    
        // we try to load both models with the info we get from post
        if($organization->load($organization) && $employee->load($organization)) {
            // we begin a db transaction
            $transaction = $organization->db->beginTransaction();
    
            try {
                // Try to save the organization
                if(!$organization->save()) {
                    throw new \Exception( 'Saving Organization Error' );
                }
    
                // Assign the organization id and other values to the employee
                $employee->organization_id = $organization->id;
                ...
    
                // Try to save the employee
                if(!$employee->save()) {
                    throw new \Exception( 'Saving Employee Error' );
                }
    
                // We use setFlash to set values in the session.
                // Important to add a 3rd param as false if you want to access these values without deleting them.
                // But then you need to manually delete them using removeFlash('organization_id')
                // You can use getFlash('organization_id'); somewhere else to get the value saved in the session.
                Yii::$app->session->setFlash('organization_id', $organization->id)
                Yii::$app->session->setFlash('employee_id', $employee->id)
                ...
    
                // we finally commit the db transaction
                $transaction->commit();
                return $this->redirect(['somewhere_else']);
            }
            catch(\Exception e) {
                // If we get any exception we do a rollback on the db transaction
                $transaction->rollback();
            }
        }
    
        return $this->render('the_view', [
            'organization' => $organization,
            'employee' => $employee,
        ]);
    }
    

    The view file:

    <?php
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    ?>
    
    <?php $form = ActiveForm::begin() ?>
    
        <?= $form->field($organization, 'some_organization_field') ?>
    
        <!-- More Fields -->
    
        <?= $form->field($employee, 'some_employee_field') ?>
    
        <!-- More Fields -->
    
        <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
    
    <?php ActiveForm::end() ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办