thiago0112
(Thiago 0112)
February 15, 2013, 12:28pm
1
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] =>
)
mdomba
(Maurizio Domba Cerin)
February 16, 2013, 8:21pm
2
You need to manually assign the foto names to the desired attributes.
thiago0112
(Thiago 0112)
February 18, 2013, 11:59am
3
that doesen’t work too, i’ve tried already with a test value and nothing
mdomba
(Maurizio Domba Cerin)
February 18, 2013, 12:09pm
4
can you post your code for that… [size=2]when you assign manually any string to $model->foto1 that text should get saved.[/size]
thiago0112
(Thiago 0112)
February 19, 2013, 12:30pm
5
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?
mdomba
(Maurizio Domba Cerin)
February 19, 2013, 12:41pm
6
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
thiago0112
(Thiago 0112)
February 19, 2013, 4:59pm
7
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 =/
mdomba
(Maurizio Domba Cerin)
February 19, 2013, 8:20pm
8
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.
thiago0112
(Thiago 0112)
February 20, 2013, 12:19pm
9
it is after the loop, in fact i made a "helper" for upload in components and the loop is in there, not in controller
mdomba
(Maurizio Domba Cerin)
February 20, 2013, 12:29pm
10
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]
thiago0112
(Thiago 0112)
February 20, 2013, 1:06pm
11
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
mdomba
(Maurizio Domba Cerin)
February 20, 2013, 1:10pm
12
Check if you have enabled the ajax validation for this form, if so you need to uncoment the line that performs the ajaxvalidation…
thiago0112
(Thiago 0112)
February 20, 2013, 1:24pm
13
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/
mdomba
(Maurizio Domba Cerin)
February 20, 2013, 1:55pm
14
Note that Gii by default sets enableAjaxValidation to false.
thiago0112
(Thiago 0112)
February 20, 2013, 2:03pm
15
i downloaded a translated Gii because im Brazilian so maybe thats why. But now its perfectly working thanks
thiago0112
(Thiago 0112)
February 22, 2013, 2:03pm
16
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?