JKLM
JKLM

Reputation: 1520

Yii2 : How to generate User ID from given string in 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(); ?>

Its my form I want to create client code from company name and current date. on submit in client_code field it should get result of first five letters from company_name field + date(day & month)

Controller-

UPDATE

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,
            ]);
        }
    }

Create

 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,

            ]);
        }
    }

Upvotes: 0

Views: 1559

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

try this In view simply get the company_name

view

<?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(); ?>                

In controller obtain the data posted and then build the user_is

Controller (create action)

 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,

        ]);
    }
}

Upvotes: 1

Related Questions