I Couldn't save in 2 models

Hey every one.

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,

            ]);

        }

    }


...

}

Could someone give me a hint/help?

Try in your controller

use yii\base\Model;

Just to complete RCJ answer

the line (in both update and insert)




&& Model::validateMultiple([$model, $modelMacroprocessoImpacto])) {



need to be namespaced or you should add the use as suggested by RCJ.

In controller the default namespace is in the controller folder




namespace app\controllers;



Thanks Guys.

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.

Updatating…

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’)

    -&gt;listBox(&#036;macroprocessos, ['multiple' =&gt; true, 'size' =&gt; 7])


    -&gt;label('Macroprocesso');[/b].

Some hint to give me? I still search a solution.

Just for confirmation. Does your FaixaImpacto have many Macroprocessos via the junction table MacroprocessoImpacto ?

If yes, then please take a look at the following wiki.

http://www.yiiframework.com/wiki/836/how-to-use-listbox-and-checkboxlist/

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?

Hi Cálcio.

I am working with Dynamicform and I am saving Multiples models. Maybe this can help you.

In models defined this




 <?php


namespace frontend\models;


use Yii;

use yii\helpers\ArrayHelper;


class Model extends \yii\base\Model

{

    /**

     * Creates and populates a set of models.

     *

     * @param string $modelClass

     * @param array $multipleModels

     * @return array

     */

    public static function createMultiple($modelClass, $multipleModels = [])

    {

        $model    = new $modelClass;

        $formName = $model->formName();

        $post     = Yii::$app->request->post($formName);

        $models   = [];


        if (! empty($multipleModels)) {

            $keys = array_keys(ArrayHelper::map($multipleModels, 'idfecha', 'idfecha'));

            $multipleModels = array_combine($keys, $multipleModels);

        }


        if ($post && is_array($post)) {

            foreach ($post as $i => $item) {

                if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {

                    $models[] = $multipleModels[$item['id']];

                } else {

                    $models[] = new $modelClass;

                }

            }

        }


        unset($model, $formName, $post);


        return $models;

    }

}




and the controller




 use frontend\models\Model;



This line should not give more problems




 && Model::validateMultiple([$model, $modelMacroprocessoImpacto])) {



A soft of, yes. But the key point is that you have to handle an array of Macroprocesso IDs.

You can extend your FaixaImpacto model to have an extra field for it.




class FaixaImpacto extends ActiveRecord {

...

public $macroprocesso_ids; // an array of Macroprocesso IDs

...



I mean that the 1st model integrated with the 4th in the wiki.

I solved my problem write it by hand. The way that Yii shows, doens’t work to me.

In my model I create 2 methods that help me solve my problem.


public function saveInBatch()

    {

        $deleteMacroprocessoImpacto = MacroprocessoImpacto::deleteAll('FAIXA_IMPACTO_ID = :faixaImpactoId', 

            [':faixaImpactoId' => $this->FAIXA_IMPACTO_ID]);

        

        $listMacroprocessoFaixaImapcto = $this->buildArrayToSave();

                        

        Yii::$app->db->createCommand()->batchInsert(

            MacroprocessoImpacto::tableName(), 

            $this->attributes(), 

            $listMacroprocessoFaixaImapcto)

        ->execute();

    }


private function buildArrayToSave()

    {

        $list = [];

        

        foreach (Yii::$app->request->post()['MacroprocessoImpacto']['MACROPROCESSO_ID'] as $macroprocessoId) {

            $list[] = [$macroprocessoId, $this->FAIXA_IMPACTO_ID];

        }

        

        return $list;        

    }