How to save data from a CArrayDataProvider in the database?

0

Hello, I have a CArrayDataProvider and I wanted to save your data to a table via a model, I already tried it in several ways and did not get results, what would be the best way to save it?

My last attempt so far:

                    foreach($usuarioItensAvulso as $value)
                    {
                        $model->cod_material = $value["cod_material"];
                        $model->des_justificativa = $value["des_justificativa"];
                        $model->des_quantidade = $value["des_quantidade"];
                        //Se as informa��es forem v�lidas, salvar e direcionar para a p�gina do Visualizar.
                        if ($model->validate())
                        {
                            //Faz a inclus�o do texto
                            $model->save();
                            //Exibe mensagem de salvo com sucesso
                            Yii::app()->user->setFlash('success', 'Salvo com sucesso.');
                        }
                    }
    
asked by anonymous 25.05.2018 / 15:36

1 answer

1

1st check if all values needed to save, if there is no $model->validate() will always return false and will never be saved. 2 ° if you are working with a $userItensAvulso array then you have to initialize your model every time you start for pq senao will be rewritten the first one to be inserted

foreach($usuarioItensAvulso as $value)
{
    $model = new Nome_do_modelo; // iniciar o teu modelo pq senao vai ser reescrito o primeiro a ser insertado

    $model->cod_material = $value["cod_material"];
    $model->des_justificativa = $value["des_justificativa"];
    $model->des_quantidade = $value["des_quantidade"];

    if ($model->validate())
    {
        $model->save();
        Yii::app()->user->setFlash('success', 'Salvo com sucesso.');
    }
}

It would be nice to be able to see all the code of this function and tmb the code of your model more specifically the function of the rules to be able to orient better on what you are doing

    
03.07.2018 / 21:05