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 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.
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.
– Pedro Paulo
Hello Peter, corrected, thanks for the tip.
– Victor Augusto
For nothing man, tmj!
– Pedro Paulo