Hi All,
I’m completely stuck with one problem, I think that I’m missing something and it must be really stupid error, but… I can’t find the solution and I have tried, I think, with everything.
I have a form where I display a partial view, this partial view contains a table with a ajax button which launch an action, (no problem for now). Once the action is executed, this refresh the table (with a renderPartial), but… I can’t load the correct rows of my model!!!
For better understanding…
Here is my form with the partial view (views\post\_form.php):
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'post-form',
'type'=>'vertical',
'enableAjaxValidation' => false,
//This is very important when uploading files
'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="field">
<?php echo $form->textFieldRow($model,'title',array('size'=>80,'maxlength'=>128)); ?>
</div>
<div class="field">
<?php echo $form->textAreaRow($model,'content',array('rows'=>10, 'cols'=>70)); ?>
</div>
<div class="field">
<?php echo $form->dropDownListRow($model,'status',PostLookup::items('PostStatus')); ?>
</div>
//Here I render a table
<?php $this->renderPartial('/postmedia/_update', array('model' => $model->medias)); ?>
<div class="actions field">
<?php $this->widget('bootstrap.widgets.TbButton',array(
'buttonType'=>'submit',
'type'=>'success',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
'size'=>'large',
//'htmlOptions'=>array('class'=>'btn btn-success btn-medium')
)); ?>
</div>
<?php $this->endWidget(); ?>
Here the partial view (\views\postMedia\_update.php):
<table class="table striped" id="down">
<tbody class="images"></tbody>
<?php foreach($model as $photo) : ?>
<tr class="fade in" style="height: 53px;">
<td class="preview">
<a href="<?php $photo->url . $photo->filename?>" title="<?php echo $photo->filename; ?>" rel="gallery" download="<?php echo $photo->filename; ?>"></a>
</td>
<td class="name">
<a href="<?php echo Yii::app()->baseUrl . $photo->url . $photo->filename?>" title="<?php echo $photo->filename; ?>" rel="image" download="<?php echo $photo->filename; ?>"><?php echo $photo->filename; ?></a>
</td>
<td class="size"><span><?php echo $photo->size; ?></span></td>
<td class="button">
<?php $this->widget('bootstrap.widgets.TbButton',array(
'buttonType'=>'ajaxButton',
'type'=>'danger',
'label'=>'Remove',
'size'=>'normal',
'icon'=>'icon-trash icon-white',
'htmlOptions'=>array(
'href'=>Yii::app()->createUrl('/postmedia/ajaxdelete', array('id'=>$photo->id)),
),
'ajaxOptions'=>array(
'update'=>'#down',
'url'=>Yii::app()->createUrl('/postmedia/ajaxdelete', array('id'=>$photo->id)),
'type'=>'POST',
),
)); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
And here the action (\controllers\PostMediaController.php):
public function actionAjaxDelete()
{
Yii::log("ANTES 0 id: " .$_GET['post_id'], "info");
if(Yii::app()->request->isPostRequest && isset($_GET['id']))
{
//$media = $this->loadModel($id);
$media = PostMedia::model()->findByPk($_GET['id']);
Yii::log("ANTES 1 media->post_id: " .$media->post_id, "info");
$medias=new CActiveDataProvider(PostMedia::model(), array(
'criteria'=>array(
'condition'=>'post_id='.$media->post_id,
),
));
//$medias = Post::model()->findByPk($media->post_id);
foreach($medias as $photo):
Yii::log("MEDIA id: " .$photo->id, "info");
Yii::log("MEDIA url: " .$photo->url, "info");
Yii::log("MEDIA filename: " .$photo->filename, "info");
Yii::log("MEDIA post_id: " .$photo->post_id, "info");
endforeach;
$this->loadModel($_GET['id'])->delete();
Yii::log("ANTES 2", "info");
echo $this->renderPartial('_update', array('model'=>$medias), true);
return;
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
As you can see, I perform an ‘echo’ with the partial view and the model instance, but the model instance is not well loaded, it seems loaded with garbage, as it returns some empty rows, I see it because it writes this on the log:
2013/03/17 04:53:02 [info] [application] ANTES 0 id:
2013/03/17 04:53:02 [info] [application] ANTES 1 media->post_id: 21
2013/03/17 04:53:02 [info] [application] MEDIA id:
2013/03/17 04:53:02 [info] [application] MEDIA url:
2013/03/17 04:53:02 [info] [application] MEDIA filename:
2013/03/17 04:53:02 [info] [application] MEDIA post_id:
2013/03/17 04:53:02 [info] [application] MEDIA id:
2013/03/17 04:53:02 [info] [application] MEDIA url:
2013/03/17 04:53:02 [info] [application] MEDIA filename:
2013/03/17 04:53:02 [info] [application] MEDIA post_id:
2013/03/17 04:53:02 [info] [application] MEDIA id:
2013/03/17 04:53:02 [info] [application] MEDIA url:
2013/03/17 04:53:02 [info] [application] MEDIA filename:
2013/03/17 04:53:02 [info] [application] MEDIA post_id:
2013/03/17 04:53:02 [info] [application] ANTES 2
If someone could tell me what I’m doing wrong… It would be really appreciate, I have tried everything and I don’t get this simple thing! :S