我有一个简单的控制器,用于将行添加到表中,但是如果失败,我希望与之保持同一页面,但仅带有Flash警告消息。
class WarehouseAddressesController{
public function itemToAdd($id = null){
$exists = $this->ItemsAddress->find('first', array('conditions' => array(
'warehouse_addresses_id' => $this->request->data['WarehouseAddress']['id'],
'items_id' => $this->request->data['Items']['itemToAdd'])));
if($this->request->is(array('post')) && empty($exists)){
$data = array(
'warehouse_addresses_id' => $this->request->data['WarehouseAddress']['id'],
'items_id' => $this->request->data['Items']['itemToAdd']
);
if ($this->ItemsAddress->save($data, array('validate' => 'first',))) {
$this->Flash->success(__('This item has been successfully added to the requested address.'));
$this->redirect($this->referer());
}
if(!empty($exists)){
$this->Flash->warning(__('This item already exists on this address'));
$this->redirect($this->referer());
}
else {
$this->Flash->error(__('This item could not be added to this address. Please, try again.'));
$this->redirect($this->referer());
}}}}
保存项目时,它会像使用$ this-> referer一样保留在同一页面上,但是当项目存在时,它显示错误错误:找不到WarehouseAddressesController :: itemToAdd()的视图。 但是我想留在页面上,只是显示页面存在的错误。
答案 0 :(得分:0)
我只需要添加return $ this-> redirect($ this-> referer());在第一个if块中,在最后一个if块中。