When inserting record this is duplicate automatically

(russo89) #1

this form receives + - 400 records daily, very rarely when making a record this is automatically duplicated with a difference of 1 or 2 seconds.


public function actionCreate()
    $model = new Nums();
    $model->scenario = Nums::SCENARIO_CREATE;

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

        try {
            if ($model->treatment == 3 && ($model->cc == null || $model->cc == '')) {
                if ($EmailTo = filter_var($model->idUser->email,FILTER_SANITIZE_EMAIL))
                    ->setCc(array('xxxxxx', 'xxxxxx', 'xxxxxx'))
                    ->setTextBody('Plain Text content')
                        "Email Text Example")
            }elseif ($model->treatment == 3 && ($model->cc != null && $model->cc != '')){
                if ($EmailTo = filter_var($model->idUser->email,FILTER_SANITIZE_EMAIL))
                if($EmailCC = filter_var($model->cc,FILTER_SANITIZE_EMAIL))
                    ->setCc(array($EmailCC, 'xxxxxx', 'xxxxxx', 'xxxxxx'))
                    ->setTextBody('Plain Text content')
                        "Other Email Text Example")
            return $this->redirect(['create']);

        }catch(Swift_SwiftException $exception){
            Yii::$app->session->setFlash('warning', "Email not sent");
            return $this->redirect(['view', 'id' => $model->id]);

    } else {
        return $this->render('create', [
            'model' => $model,

I know the code is confusing but will it be the code problem? Could be server problem? is that it happens a few times, in 400 it happens only 2 times.

i have one Conditional Validator like this

['id_user', 'required',
            'when' => function ($model) {
                return $model->treatment == '2' OR $model->treatment == '3';
            'whenClient' => "function (attribute, value) {
                return $('#treatment').val() == '2' || $('#treatment').val() == '3';

            'on' => self::SCENARIO_CREATE

beforeValidate() in common\models\Nums.php

public function beforeValidate()
    if ($this->scenario == self::SCENARIO_CREATE){
        $this->a = Yii::$app->user->identity->id;
        $this->date = date('Y-m-d H:i:s');
    }else {
        $this->a = $this->a;
        $this->date = $this->date;

    return parent::beforeValidate(); // TODO: Change the autogenerated stub

(Mehdi Achour) #2

Sometime, the problem lays between the chair and the keyboard:
What’s probably going on, is that the user clicks the submit button, but the page takes some time to load. So he hits submit again.
I also know some (old) people who double click links and button on the internet, because of windows explorer habits…
You could disable the submit button as soon as it’s clicked to prevent that.

(Mehdi Achour) #3

I just noticed that you’re sending an email in the controller, before rendering the response. Sometimes the connection to the SMTP may be slower than usual… So what I described in my previously reply is most likely to happen.

You should consider using the queue for sending emails, so that it doesn’t affect your response time.

(russo89) #4

should not be because if the first or second condition if is false , save without sending the email…
thanks for the yii2-queue.

(russo89) #5

@machour you are right! I did not want to believe but the problem was because of the delay of the email the user clicked twice on the button submit. :joy:

(Mehdi Achour) #6

Hahaha :joy:
:champagne: Congrats both on fixing the bug and admitting this !