Jaya Kumari Jaya Kumari - 3 months ago 37
jQuery Question

how to update multiple image that will store in different table using single action in yii2

In this update action code only update data and upadte image on folder but does not updating another table tabl4e image

public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->validate()) {
$model->save();
$cid = $model->id;

if (isset($_FILES['attr_name']['tmp_name'])) {
$photos = UploadedFile::getInstancesByName('attr_name');
$imgName = array();
if (isset($photos) && count($photos) > 0) {
$DD = 0 ;

foreach ($photos as $image => $pic) {

$imagepath = Yii::getAlias('@image').'/';
$imageFileName = rand(10, 100).$pic->name;
$imageFile = $imagepath.$imageFileName;
if ($pic->saveAs($imageFile)) {
$imgName[] = $imageFileName;
} else {
//echo 'Cannot upload!';
}

$Image = new Image();
$Image->c_id = $cid;
$Image->image = $imageFileName;
$img = $Image->image;

if (isset($_POST['deletedImg'])) {
$pics = $_POST['deletedImg'];

foreach ($pics as $img1) {
$image = $img1;
unlink(Yii::getAlias('@image').'/'.$image);
}
} else {
$Image->image = $img;
}

if ($Image->validate()) {
$Image->save();
} else {
print '<pre>';
print_r($Image->errors);
exit;
}
unset($Image);
}
}

return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}


jQuery



In this code will getting multiple file button and store and delete multiple images, i am getting delete image id, how to use this id on update action, i don't have any idea.

$(".removeBtn").click(function(event){
var img =$(this).attr("data-id");
var id = $(this).attr('id');
var hiddenfield= '<input type="hidden" value="'+img+'" name="deletedImg[]" id=""/>';
$("#deleted-img").append(hiddenfield);
$(this).parent().parent().remove();
});
$(".btnAddRec").click(function(){
var clonedHTML = $( "#fieldsRow" ).html();
clonedHTML = clonedHTML.replace("<!--BUTTON-->",'<button type="button" class="removeBtn btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash"></i> Delete</button>');
$("#cloned").append('<div class="row" id="fieldsRow">'+clonedHTML+'</div>');
$(".removeBtn").click(function(event){
$(this).parent().parent().remove();
});
$("#cloned #fieldsRow:last-child input ").val('');
});

Answer
public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post())) {
        if($model->validate())
        {
            $model->save();
            $cid = $model->id;
            $Image = Image::find()->where(['c_id' => $cid])->asArray()->all();

            if(!empty($Image)){
                if(isset($_FILES['attr_name']['tmp_name']) > 0){
                     if(isset($_POST['deletedImg']))
                        {
                            $pics = $_POST['deletedImg'];
                           // echo "<pre>";print_r($pics);exit;
                            foreach($pics as $img1)
                            {
                                $image = $img1;
                                unlink(Yii::getAlias('@image').'/'.$image);
                            }

                            $connection = \Yii::$app->db;
                            $delRec = $connection->createCommand(" delete image FROM image WHERE image='$image'");
                            $delRecExec = $delRec->query();
                        }
                    $photos = UploadedFile::getInstancesByName('attr_name');
                    $imgName = array();
                    if(isset($photos) && count($photos) > 0)
                    {

                       foreach ($photos as $image => $pic) {

                        $imagepath = Yii::getAlias('@image').'/';
                        $imageFileName = rand(10,100).$pic->name;
                        $imageFile = $imagepath.$imageFileName;
                        if ($pic->saveAs($imageFile)) 
                        {
                            $imgName[] = $imageFileName;
                        }
                        else
                        {
                            //echo 'Cannot upload!';
                        }
                            $Image = new Image();
                           // print_r($Image);exit;
                            $Image->c_id = $cid;
                            $Image->image = $imageFileName;
                            $img = $Image->image;

                            if($Image->validate()){
                            $Image->save();
                            }else{
                                print '<pre>';print_r($Image->errors);
                                exit;
                            }
                            unset($Image);
                        }
                    }

                    return $this->redirect(['view', 'id' => $model->id]); 
                }


            }
        }
        else 
        {
            return $this->render('create', [
                        'model' => $model,
                    ]);
        }   
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}
Comments