Yii Framework Forum: Upload Image Issue - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Upload Image Issue Rate Topic: -----

#1 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 15 February 2013 - 07:28 AM

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] => 
)


0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 16 February 2013 - 03:21 PM

You need to manually assign the foto names to the desired attributes.
Find more about me.... btw. Do you know your WAN IP?
1

#3 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 18 February 2013 - 06:59 AM

View PostMaurizio Domba, on 16 February 2013 - 03:21 PM, said:

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
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 18 February 2013 - 07:09 AM

can you post your code for that... when you assign manually any string to $model->foto1 that text should get saved.
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 19 February 2013 - 07:30 AM

View PostMaurizio Domba, on 18 February 2013 - 07:09 AM, said:

can you post your code for that... when you assign manually any string to $model->foto1 that text should get saved.

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?
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 19 February 2013 - 07:41 AM

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
Find more about me.... btw. Do you know your WAN IP?
1

#7 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 19 February 2013 - 11:59 AM

View PostMaurizio Domba, on 19 February 2013 - 07:41 AM, said:

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 =/
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 19 February 2013 - 03:20 PM

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.
Find more about me.... btw. Do you know your WAN IP?
0

#9 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 20 February 2013 - 07:19 AM

View PostMaurizio Domba, on 19 February 2013 - 03:20 PM, said:

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
0

#10 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 20 February 2013 - 07:29 AM

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
- Do you use ajax? In this case check that you do not save on ajax validation.
Find more about me.... btw. Do you know your WAN IP?
0

#11 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 20 February 2013 - 08:06 AM

View PostMaurizio Domba, on 20 February 2013 - 07:29 AM, said:

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
- Do you use ajax? In this case check that you do not save on ajax validation.

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
0

#12 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 20 February 2013 - 08:10 AM

Check if you have enabled the ajax validation for this form, if so you need to uncoment the line that performs the ajaxvalidation...
Find more about me.... btw. Do you know your WAN IP?
0

#13 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 20 February 2013 - 08:24 AM

View PostMaurizio Domba, on 20 February 2013 - 08:10 AM, said:

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/
0

#14 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 20 February 2013 - 08:55 AM

Note that Gii by default sets enableAjaxValidation to false.
Find more about me.... btw. Do you know your WAN IP?
0

#15 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 20 February 2013 - 09:03 AM

View PostMaurizio Domba, on 20 February 2013 - 08:55 AM, said:

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
0

#16 User is offline   thiago.silva 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 13-February 13

Posted 22 February 2013 - 09:03 AM

View PostMaurizio Domba, on 16 February 2013 - 03:21 PM, said:

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

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?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users