Upload Image Issue

I’m trying to upload four images and it saves on the server but not in DB. Dont know why but the image attribute dont receive the value from variable.

Controller action:




if(isset($_POST['Noticias']))

		{

			$dados=$_POST['Noticias'];

                        unset($dados['fotos']);

                         $fotos = CUploadedFile::getInstancesByName('Noticias');

                         

                        if(isset($fotos) && count($fotos) > 0) 

                        {

                            foreach($fotos as $c => $foto)

                            {

                                $nome_foto = $foto->name;

                                $nome_foto = explode('.',$nome_foto);

                                $nome_foto = time().uniqid().'.'.$nome_foto[1];

                                

                                if($foto->saveAs(Yii::getPathOfAlias('webroot').'/arquivos/fotos/noticias/' . $nome_foto))

                                {

                                     $c++;

                                    $dados['foto' . $c] = $nome_foto;

                                }else

                                {

                                    throw new CException(Yii::t('yii','Erro ao fazer upload do arquivo "{nome}".',

                                            array('{nome}'=>$nome_foto)));

                                }

                            }

                            

                        }

                        $model->attributes = $dados;

                        print_r($dados);

                        print_r("<br/><br/>");

			print_r($model->attributes);

                        die();

                        if($model->save())

				$this->redirect(array('view','id'=>$model->id_noticia));

		}



Print "dados" and "attributes":




//Dados

Array

(

    [titulo] => teste

    [categoria] => 1

    [data] => 3

    [id_album] => 3

    [resumo] => 3

    [conteudo] => 3

    [fonte] => 3

    [legenda1] => 

    [legenda2] => 

    [legenda3] => 

    [legenda4] => 

    [newsletter] => 

    [foto1] => 1360931081511e29098511a.jpg

)


//Attributes

Array

(

    [titulo] => teste

    [categoria] => 1

    [data] => 3

    [id_album] => 3

    [resumo] => 3

    [conteudo] => 3

    [fonte] => 3

    [legenda1] => 

    [legenda2] => 

    [legenda3] => 

    [legenda4] => 

    [newsletter] => 

    [id_noticia] => 

    [foto1] => 

    [foto2] => 

    [foto3] => 

    [foto4] => 

)




You need to manually assign the foto names to the desired attributes.

that doesen’t work too, i’ve tried already with a test value and nothing

can you post your code for that… [size=2]when you assign manually any string to $model->foto1 that text should get saved.[/size]

sorry, i confused myself. mannualy it works but i have four images and need to do it as a loop, how can i? And how can i resize that pictures too?

Resizing pictures is another issue, check for some extensions…

As for the names… you need to assign them manually try with something like this in a loop




$name='foto'.$i;

$model->$name = $photoname;



Where $i is the counter from 1 to 4

making that way work for upload the picture but save 4 times the model in db =/

This all depends on your code… you probably save inside the loop but this loop should be only for getting/setting the file names and only after the loop you need to save the model.

it is after the loop, in fact i made a "helper" for upload in components and the loop is in there, not in controller

Well you need to find where the saving is taking place… without looking at your code and the saved models I can’t help you more than this…

Some ideas:

  • Check if all the 4 saved models are the same - from that you can get an idea when those records gets saved

[size=2] - Do you use ajax? In this case check that you do not save on ajax validation.[/size]

Trying too, but i’m not understanding the behavio, look:




public function actionCreate()

	{

                

		$model=new Noticias;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

		

                if(isset($_POST['Noticias']))

		{

			$model->attributes=$_POST['Noticias'];

                        //$upload = new UploadFotos();

                        

                         //$upload->setThumb(array('width'=>180));

                         

                         //$model=$upload->Upload($model);

                  

                         if($model->save())

                            $this->redirect(array('view','id'=>$model->id_noticia));

		}

               

		$this->render('create',array(

			'model'=>$model,

		));

	}



i commented with the management upload codes, so all the files is same generated by Gii, and still save more than once

Check if you have enabled the ajax validation for this form, if so you need to uncoment the line that performs the ajaxvalidation…

You’re right, again rs. I still do not understand why Gii generate a form with ajaxValidation enabled and a controller with ajaxValidation commented but its working thanks again o/

Note that Gii by default sets enableAjaxValidation to false.

i downloaded a translated Gii because im Brazilian so maybe thats why. But now its perfectly working thanks

I have another issue like this using editMe extension, have to assign manually, so i want to ask you. Why i have to do that if the nams are right, and when i have to do that?