Ciao a tutti,
da una view vorrei aggiornare due tabelle.
Nella vista che fa l’update della tabella events, ho messo un input file per uploadare una o più immagini nella tabella events_images, legare quindi una o più immagini ad un evento (aggiungendo più immagini conlo stesso event_id).
Io ho creato i model ed i crud delle due tabelle.
poi le tabelle sono queste:
tabella events
Campo Tipo
event_id int(11)
title tinytext
content text Sì
schedule text
start_date date
end_date date
place varchar(100)
tabella events_images
Campo Tipo
file_id int(11)
name varchar(64)
description text
real_name varchar(128)
event_id int(11)
e la view _form.php di update della tabella "events" è questa:
<div class="form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'events-form',
'enableAjaxValidation' => false,
/*'htmlOptions' => array(
'enctype' => 'multipart/form-data'
)*/
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php // echo $form->textArea($model,'title',array('rows'=>6, 'cols'=>50)); ?>
<?php
$this->widget('application.extensions.tinymce.ETinyMce', array(
'name' => 'html',
'editorTemplate' => 'full',
'model' => $model,
'attribute' => 'title',
'options' => array(
'theme' => 'advanced',
'skin' => 'o2k7',
'theme_advanced_buttons1' => 'preview,bold,italic,underline,fontselect,fontsizeselect,link,justifyfull,justifyleft,justifycenter,justifyright,pasteword,pastetext,table,image,|,bullist,numlist,|,undo,redo,|,code,fullscreen',
'theme_advanced_buttons2' => '',
'theme_advanced_buttons3' => '',
),
'value' => $model->title,
));
?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'content'); ?>
<?php
$this->widget('application.extensions.tinymce.ETinyMce', array(
//'name' => 'html',
'editorTemplate' => 'full',
'model' => $model,
'attribute' => 'content',
'options' => array(
'theme' => 'advanced',
'skin' => 'o2k7',
'theme_advanced_buttons1' => 'preview,bold,italic,underline,fontselect,fontsizeselect,link,justifyfull,justifyleft,justifycenter,justifyright,pasteword,pastetext,table,image,|,bullist,numlist,|,undo,redo,|,code,fullscreen',
'theme_advanced_buttons2' => '',
'theme_advanced_buttons3' => '',
),
'value' => $model->content,
));
?>
<?php echo $form->error($model, 'content'); ?>
</div>
<!--
<div class="row">
<?php echo $form->labelEx($model,'schedule'); ?>
<?php echo $form->textArea($model,'schedule',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'schedule'); ?>
-->
<div class="row">
<?php echo $form->labelEx($model,'start_date'); ?>
<?php //echo $form->textField($model,'start_date'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'value'=>'',
'attribute' => 'start_date',
'language' => 'it',
'options'=>array(
'dateFormat'=>'dd-mm-yy',
'changeMonth' => 'true',
'changeYear' => 'true',
'showButtonPanel' => 'true',
'constrainInput' => 'false',
'showAnim' =>'slideDown'
),
/*'htmlOptions'=>array(
'class'=>'start_date'
),*/
));
?>
<?php echo $form->error($model,'start_date'); ?>
</div>
<?php echo $form->labelEx(EventsImages::model(), 'real_name'); ?>
<?php echo $form->fileField(EventsImages::model(), 'real_name', array('size' => 60, 'maxlength' => 250)); ?>
<?php echo $form->error(EventsImages::model(),'real_name'); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<!-- form -->
questo invece una delle tante prove del mio metodo update di EventsController.php
public function actionUpdate($id) {
$model = $this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Events'])) {
$model->attributes = $_POST['Events'];
// parte che interessa l'upload
$modelEventsImages = new EventsImages;
$modelEventsImages->attributes = $_POST['EventsImages'];
echo "<pre>";
echo "<br><br>\$models->attributes:<br>";
var_dump($model->attributes);
echo "<br><br>\$modelEventsImages->attributes:<br>";
var_dump($modelEventsImages->attributes);
//die();
// Aggiungo anche in update l'upload dell'immagine
if (isset($_FILES["Events"])) {
$ext = end(explode(".", $_FILES["Events"]["name"]["real_name"]));
$immagine = md5(mktime() . $_FILES["EventsImages"]["name"]["real_name"]) . '.' . $ext;
//$modelEventsImages = new EventsImages;
$modelEventsImages->real_name = CUploadedFile::getInstance($modelEventsImages, 'real_name');
$modelEventsImages->real_name->saveAs(Yii::app()->basePath . "/../images/" . $immagine);
$image = Yii::app()->image->load(Yii::app()->basePath . "/../images/" . $immagine);
$image->resize(179, 179);
$image->save();
$modelEventsImages->real_name = $immagine;
//$modelEventsImages->save();
/*if ($modelEventsImages->save())
$this->redirect(array('view', 'id' => $model->id));
*/
}
if ($model->save())
$this->redirect(array('view', 'id' => $model->event_id));
}
$this->render('update', array(
'model' => $model,
));
}
mi sono un po inceppato… perchè se stampo
var_dump($model->attributes);
var_dump($modelEventsImages->attributes);
ricevo sia i dati della form originale sia l’immagine., ma non riesco a capire come continuare…
qualsiasi suggerimento e ben accetto!
grazie mille
GIuseppe