Yii2:如何从Yii2中的给定字符串生成用户ID?

时间:2015-07-17 17:47:04

标签: php yii2

<?php $form = ActiveForm::begin(); ?> 
<?= Html::activeHiddenInput($model, 'client_code', ['value' => $result ])?>
            <?php
                $myStr  = $model => 'comany_name';
                $result = date("m-d") . "";
                for($i=0; $i<5; $i++) {
                    $result .= $myStr[$i];
                }
                echo $result ;
            ?> 
   <?= $form->field($model, 'company_name')->textInput(['maxlength' => true]) ?>
    <div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>
        <?php ActiveForm::end(); ?>

我的表单我想从公司名称和当前日期创建客户端代码。在client_code字段中提交时,它应该从company_name字段+日期(日期和月份)获得前五个字母的结果

控制器 -

更新

public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

创建

 public function actionCreate()
    {
        $model = new CreateClient();
        $employee = new Employee();

        if ($model->load(Yii::$app->request->post())) 
        {
            $model->save();

             $employee->attributes = $model->attributes;
             $employee->save();

            return $this->redirect(['view', 'id' => $model->id]);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
                'employee' => $employee,

            ]);
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个 在视图中只需获取company_name

查看

<?php $form = ActiveForm::begin(); ?> 
    <?= $form->field($model, 'company_name')->textInput(['maxlength' => true]) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>                

在控制器中获取发布的数据,然后构建user_is

控制器(创建操作)

 public function actionCreate() 
 {

    $model = new CreateClient();
    $employee = new Employee();

    if ($model->load(Yii::$app->request->post())) 
    {

        $myStr = substr($model->company_name, 0, 5);
        $model->user_id  = $myStr . date("m-d") ;
        $model->save();

         $employee->attributes = $model->attributes;
         $employee->save();

        return $this->redirect(['view', 'id' => $model->id]);
    } 
    else 
    {
        return $this->render('create', [
            'model' => $model,
            'employee' => $employee,

        ]);
    }
}