Yii crud, how to add a button to launch a model function

How to add a button in crud _form for actionUpdate?

so I can delete the saved file and upload a new

i had this function in the model

    public function deleteImage()
    { 
        unlink(Yii::app()->basePath.'/../uploads/'.$this->image);
    }

so how to start it with such a button?

<input type="button" value="delete image"/>
+3
source share
1 answer

In MVC, the controller usually takes care of user interaction, so the user does not have direct access to the model, but does so through the controller. The controller intercepts user actions and does the work, invoking the model (s). Therefore, it is better not to name the model function directly, even if it is possible.

, , , () controller, , , .

() , URL-, , , onclick. , , URL-, URL- ajax.

: 1) :

public function actionMyaction($id){
    $sampleMod=SampleModel::model()->findByPk($id);
    $sampleMod->deleteImage();
    Yii::app()->end();
}

2) : <input type="button" value="delete image"/> CHtml helper class ajaxButton.

echo CHtml::ajaxButton('Delete Image',Yii::app()->createUrl('controllername/actionname',array('id'=>$id)));

$id.

+6

All Articles