我正在调用函数的视图是index.ctp 控制器是AdvertisementsController 函数是change_status 现在的事情是该视图不属于该控制器,因此属于模型。 现在,我想获取函数中视图使用的模型名称。我该怎么办?
$ modelname = Inflector :: classify($ this-> params ['controller']); 我已经用过了,但这不会,因为多个视图属于其他模型正在调用该函数。
我也在ctp中定义了ajax数据,但没有得到。
function changeStatus(){
$('.change-status').on('change',function(){
$("#loading_image").show();
var status = $(this).val();
var userId = $(this).attr('emp-id');
var modelname = 'ClAdvertisement';
$.ajax({
type: "POST",
url: adminUrl+'advertisements/change_status',
data: {'data[User]':{'status':status,'id':userId,'modelname':modelname}},
dataType: 'json',
error:function(a,b,c) {
$("#loading_image").hide();
bootbox.alert({
message: 'Unable to process request. - ' + a,
className: "bootbox-m"
});
},
success: function (data) {
$("#loading_image").hide();
if(data.success == true){
showMsg('success',data.msg);
}else{
showMsg('error',data.msg);
}
}
});
});
}
在控制器中,我正在使用它 $ this-> request-> data ['User'] ['modelname'];
完整的功能是 公共功能change_status(){
$this->_is_user_login ();
$this->layout = false;
// $modelname = Inflector::classify( $this->params['controller']);
if($this->request->is('ajax')) {
if($this->request->is('post')) {
prd($this->request->data['modelname']);
$this->request->data[$modelname]['id'] = base64_decode($this->request->data[$modelname]['id'] );
$status = $this->request->data[$modelname]['status'];
$response = array();
if($this->$modelname->save($this->request->data)) {
$response = array('success' => true,'message' => 'Status has been updated successfully.');
}else{
$response = array('success' => false,'message' => 'Opps error please try again.');
}
}else{
$response = array('success' => false,'message' => 'Invalid request.');
}
echo json_encode($response);
die;
}else{
$this->redirect('/');
}
}
只是型号名称。