I’m having difficulties using the TabularInputManager extension.
I have an Event model, and an EventMedia model that stores all related media file paths (images, videos, and thumbnails). The models are linked with has_many and belongs_to in the models.
I tried to follow the instruction, but I find strange problems.
-
Create doesn’t work… it keeps looping back to the same screen after I hit the “create” button
-
Update Media files works partially
2a. add an media file loops back to the form with the new media file in place (but not the thumb), still can't save. I checked mysql db, no entry was created 2b. modify the media_path and media_thumb of the first item in the tabular input works, but the other one's media_thumb refuse to change though the save seem successful (media_path can be updated successfully though, strange).
Here is my code:
EventMediaManager:
<?php
class EventMediaManager extends TabularInputManager
{
protected $class = 'EventMedia';
public function getItems()
{
if (is_array($this->_items))
return ($this->_items);
else
return array(
'n0'=>new EventMedia,
);
}
public function deleteOldItems($model, $itemsPk)
{
$criteria=new CDbCriteria;
$criteria->addNotInCondition('id', $itemsPk);
$criteria->addCondition("event_id= {$model->primaryKey}");
EventMedia::model()->deleteAll($criteria);
}
public static function load($model)
{
$return= new EventMediaManager;
foreach ($model->eventMedias as $item)
$return->_items[$item->primaryKey]=$item;
return $return;
}
public function setUnsafeAttribute($item, $model) {
$item->event_id=$model->primaryKey;
}
}
EventController update and create event:
public function actionCreate()
{
$model=new Event;
$mediaManager=new eventMediaManager();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Event']))
{
$model->attributes=$_POST['Event'];
$mediaManager->manage($_POST['EventMedia']);
if (!isset($_POST['noValidate']))
{
$valid=$model->validate();
$valid=$mediaManager->validate($model) && $valid;
if($valid)
{
$model->save();
$mediaManager->save($model);
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
'mediaManager'=>$mediaManager,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// $mediaManager = EventMediaManager::load($model);
$mediaManager = new EventMediaManager();
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model, $mediaManager);
if(isset($_POST['Event']))
{
$model->attributes=$_POST['Event'];
$mediaManager->manage($_POST['EventMedia']);
if (!isset($_POST['noValidate']))
{
$valid = $model->validate();
$valid = $mediaManager->validate($model) && $valid;
if($valid)
{
if($model->save()){
$mediaManager->save($model);
$this->redirect(array('view','id'=>$model->id));
}
}
}
} else{
$mediaManager = EventMediaManager::load($model);
}
$this->render('update',array(
'model'=>$model,
'mediaManager'=>$mediaManager,
));
}
_form (the tabular part):
<div class="form">
…
<div class="row">
<?php echo $form->labelEx($model,'link'); ?>
<?php echo $form->textField($model,'link',array('size'=> 60, 'maxlength' => 4096)); ?>
<?php echo $form->error($model,'link'); ?>
</div>
<h2>Medias</h2>
<table>
<tr>
<th><?php echo EventMedia::model()->getAttributeLabel('media_title')?></th>
<th><?php echo EventMedia::model()->getAttributeLabel('media_path').' (width<900)'?></th>
<th><?php echo EventMedia::model()->getAttributeLabel('media_thumb').' (width=170)'?></th>
<th><?php echo CHtml::link(
'add',
'#',
array(
'submit'=>'',
'params'=>array('EventMedia[command]'=>'add',
'noValidate'=>true)));?></th>
</tr>
<?php foreach($mediaManager->items as $id=>$media):?>
<?php $this->renderPartial('_formEvent', array('id'=>$id, 'model'=>$media, 'form'=>$form));?>
<?php endforeach;?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
_formEvent:
<tr>
<td>
<?php
//echo $form->labelEx($model, 'imageTitle');
echo $form->textField($model, "[$id]media_title", array('size' => 45, 'maxlength' => 4096));
echo $form->error($model, 'media_title');
?>
</td>
<td>
<?php
echo CHtml::image($model->media_path, (String)$model->id, array('class'=>'media', 'width'=>200));
?>
</td>
<td>
<?php
echo CHtml::image($model->media_thumb, (String)$model->id, array('class'=>'mediaThumb', 'width'=>170));
?>
</td>
</tr>
<tr>
<td></td>
<td>
<?php
//echo $form->labelEx($model, 'media_path'.' (Best width<900)');
echo $form->textField($model, "[$id]media_path", array('size' => 45, 'maxlength' => 4096));
echo $form->error($model, 'media_path');
?>
</td>
<td>
<?php
//echo $form->labelEx($model, 'media_thumb'. ' (Width = 170)');
echo $form->textField($model, "[$id]media_thumb", array('size' => 45, 'maxlength' => 4096));
echo $form->error($model, 'media_thumb');
?>
</td>
<td>
<?php
echo CHtml::link(
'Delete',
'#',
array(
'submit'=>'',
'params'=>array(
'EventMedia[command]'=>'delete',
'EventMedia[id]'=>$id,
'noValidate'=>true)
));
?>
</td>
</tr>
<tr>
<td colspan="4">
<hr />
</td>
</tr>
Can anyone help?