Serial ID not assigning to model id

Asked

Viewed 32 times

0

I’m making a website that receives for a Webservice, data from a dvd (from a site of another colleague), and creates an offer with his information. I think I’m having trouble with the MVC structuring and not with the framework itself.

public function uploadImagem($base64){
    $nomeImagem = $this->ofe_id;

    $this->file = UploadedFile::getInstance($this, 'file');

    if($this->file){
        $this->file->saveAs('imagens/'.$nomeImagem.$this->file->extension);
        $this->ofe_imagem = ('imagens/'.$nomeImagem.$this->file->extension);
    }else{
        file_put_contents('imagens/'.$nomeImagem, base64_decode($base64));
        $this->ofe_imagem = ('imagens/'.$nomeImagem);
        $this->save();
    }

}

In this role, I create an image in my Base64-based bank that I receive from his web service, and name it with the ID of the created offer.

 public function actionCreate()
{
    $model          = new Oferta();
    $categorias     = \app\models\Categoria::find()->all();
    $tipos          = \app\models\TipoOferta::find()->all();
    $subcategorias  = \app\models\Subcategoria::find()->all();

    if ($model->load(Yii::$app->request->post())) {
        $conteudoDaImagem = Yii::$app->request->post('imagem');


        $model->ofe_emp_id      = Yii::$app->user->identity->usu_emp_id;
        $model->ofe_cliques     = 0;
        $model->ofe_titulo      = $_POST['ofe_titulo'];
        $model->ofe_sub_id      = $_POST['subcategoria'];
        $model->ofe_tipo_id     = $_POST['ofe_tipo_id'];
        $model->ofe_redir       = $_POST['ofe_redir'];

        if ($model->uploadImagem($conteudoDaImagem) || $conteudoDaImagem){
            $model->save();
            return $this->redirect(['view', 'id' => $model->ofe_id]);
        }else{
            return $this->redirect(['index']);
        }

    }

    return $this->render('create', [
        'model' => $model,
        'categorias' => $categorias,
        'tipos' => $tipos,
        'subcategorias' => $subcategorias,
    ]);

}

In this action in the Offer controller, I assign to the template the values I receive from the form and save again.

My Offer column:inserir a descrição da imagem aqui

My problem has been this: Whenever I try to create a new offer, the ofe_id that would be the offer ID is blank, even if in the bank, the column is as Serial type. This ends up affecting the image nomenclature, just as it does not actually allow you to create an offer, because the Ofe_id column must be filled.

I think I’m having problems with MVC structuring and not with Yii2 itself, if anyone can help me, I’d appreciate it from now on.

  • 1

    Hello Victor, good? Change your images to your code, because here in the forum we use your own code to help you. Try to read the policies of the site to better understand the functioning of the publications. Hug.

  • Hello Peter, corrected, thanks for the tip.

  • For nothing man, tmj!

1 answer

1


Your model->save() has to come before $model->uploadImage Why the $id does not exist until that model is saved in the database. Your code rearranged to work:

    $model->ofe_emp_id      = Yii::$app->user->identity->usu_emp_id;
    $model->ofe_cliques     = 0;
    $model->ofe_titulo      = $_POST['ofe_titulo'];
    $model->ofe_sub_id      = $_POST['subcategoria'];
    $model->ofe_tipo_id     = $_POST['ofe_tipo_id'];
    $model->ofe_redir       = $_POST['ofe_redir'];
    $model->save();

    $conteudoDaImagem = Yii::$app->request->post('imagem');
    if ($model->uploadImagem($conteudoDaImagem) || $conteudoDaImagem){
        return $this->redirect(['view', 'id' => $model->ofe_id]);
    }else{
        return $this->redirect(['index']);
    }
  • I modified here and it worked, obg for the help

  • Not at all. Give me a vote for an answer if you can.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.