Serial ID not assigning to model id

0

I'm making a website that gets through a Webservice, the data from a dvd (from a colleague's website), and creates an offer with its information. I think I'm having a problem with 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 function, I create an image in my Base64 based database that I get from the webservice, and name it with the offer ID you created.

 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 Bid Controller, I give the template the values I receive from the form and save it again.

My Offer column:

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

I think I'm having problems with MVC structuring and not with Yii2 itself, if anyone can help me, thank you in advance.

    
asked by anonymous 17.10.2018 / 18:44

1 answer

1

Your model-> save () has to come before $ model-> uploadImage Why $ id does not yet exist as long as this model is not saved to the database. Your code reorganized 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']);
    }
    
17.10.2018 / 21:58