How to save data from a Carraydataprovider in the Database?

Asked

Viewed 24 times

0

Hello, I have a Carraydataprovider and wanted to save your data in a table through a model, I have tried several ways and did not get result, which 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.');
                        }
                    }

1 answer

1

1 ° 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 an array $userItensAvulso then you have to initialize your model every time you start thefor pq will not be rewritten the first 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 see all the code of this function y tmb the code of your model more specifically the function of the rules to be able to better guide what these to do

Browser other questions tagged

You are not signed in. Login or sign up in order to post.