I try save data in to models and one field I need insert/update many values, but all examples that I tried give me the same error Class ‘app\controllers\Model’ not found.
I’m trying save in a Many to Many model. Follow my models:
My Controller FaixaImpactoController
<?php
namespace app\controllers;
use Yii;
use app\models\FaixaImpacto;
use app\models\Macroprocesso;
use app\models\MacroprocessoImpacto;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* FaixaImpactoController implements the CRUD actions for FaixaImpacto model.
*/
class FaixaImpactoController extends Controller
{
...
/**
* Creates a new FaixaImpacto model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new FaixaImpacto();
$modelMacroprocessoImpacto = new MacroprocessoImpacto();
$macroprocessoList = Macroprocesso::find()->all();
if ($model->load(Yii::$app->request->post())
&& $modelMacroprocessoImpacto->load(Yii::$app->request->post())
&& Model::validateMultiple([$model, $modelMacroprocessoImpacto])) {
$model->ID = $model->getNewId();
$model->save(false);
$modelMacroprocessoImpacto->FAIXA_IMPACTO_ID = $model->ID;
$modelMacroprocessoImpacto->save(false);
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->ID]);
}
} else {
return $this->render('create', [
'model' => $model,
'modelMacroprocessoImpacto' => $modelMacroprocessoImpacto,
'macroprocessoList' => $macroprocessoList,
]);
}
}
/**
* Updates an existing FaixaImpacto model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelMacroprocessoImpacto = new MacroprocessoImpacto();
$macroprocessoList = Macroprocesso::find()->all();
if ($model->load(Yii::$app->request->post())
&& $modelMacroprocessoImpacto->load(Yii::$app->request->post())
&& Model::validateMultiple([$model, $modelMacroprocessoImpacto])) {
$model->save(false);
$modelMacroprocessoImpacto->FAIXA_IMPACTO_ID = $model->ID;
$modelMacroprocessoImpacto->save(false);
return $this->redirect(['view', 'id' => $model->ID]);
} else {
return $this->render('update', [
'model' => $model,
'modelMacroprocessoImpacto' => $modelMacroprocessoImpacto,
'macroprocessoList' => $macroprocessoList,
]);
}
}
...
}
I have a question about use yii\base\Model;. The ActiveRecord must have the validateMultiple() method. I tried use ActiveRecord::validateMultiple() and it not work.
To me it’s strange because I need to implement 2 models, it’s no sense.
When I removed the Model::validateMultiple() I can save. But I’m still face a problem with the save. I want save items from [b]$form->field($modelMacroprocessoImpacto, ‘MACROPROCESSO_ID’)
It doesn’t show exactly what you want to do. It just tries to establish the relations between an existing item and its related items, while you are trying to create an item and define the relations at the same time. But I hope it will be of some help.
Yes, it’s a relations Many to Many. My scenario is, in FaixaImpacto form I filled their fileld and a field from MacroprocessoImpacto, I need to insert or update one Id from FaixaImpacto and Many from MacroprocessoImpacto.
idFaixaImpacto | idMacroprocessoImpacto
1 | 4
1 | 8
2 | 4
You suggest me use a Model instead of ActiveRecord in MacroprocessoImpacto?