2015-04-12 51 views
1

我是新的蛋糕抱歉,如果这是一个简单的问题。在cakephp 3中的Flash消息它不起作用

当我完成保存数据,我尽量表现有关,如果用户保存或无法保存信息的消息,告诉我下一个错误:

错误:调用一个成员函数错误( )上的非对象 文件C:\瓦帕\ WWW \ PROYECTO \ SRC \控制器\ AdministradorsController.php 行:76

AdministradorsController extends AppController

public function add() 
{ 
    $administrador = $this->Administradors->newEntity(); 
    if ($this->request->is('post')) { 

     $this->loadModel('Personas'); 
     $persona = $this->Personas->newEntity(); 
     $persona->rut = $this->request->data['Personas']['rut']; 
     $persona->sexo = $this->request->data['Personas']['sexo']; 
     $persona->nombre = $this->request->data['Personas']['nombre']; 
     $persona->apellido_paterno = $this->request->data['Personas']['apellido_paterno']; 
     $persona->apellido_materno = $this->request->data['Personas']['apellido_materno']; 
     $persona->direccion = $this->request->data['Personas']['direccion']; 
     $persona->telefono_fijo = $this->request->data['Personas']['telefono_fijo']; 
     $persona->telefono_movil = $this->request->data['Personas']['telefono_movil']; 
     $persona->fecha_nacimiento = $this->request->data['Personas']['fecha_nacimiento']; 
     $persona->email = $this->request->data['Personas']['email']; 
     $persona->comuna_id = $this->request->data['Personas']['comuna_id']; 

     if(!$this->Personas->save($persona)){ 

      $this->Flash->error('The administrador could not be saved. Please, try again.'); 
     } 



     $administrador = $this->Administradors->newEntity(); 

     $administrador->persona_id = $persona->id; 

     if(!$this->Administradors->save($administrador)){ 

       $this->Flash->error('The administrador could not be saved. Please, try again.'); 
     } 

     $this->loadModel('Users'); 
     $user = $this->Users->newEntity(); 
     $user->username = $persona->email; 
     $user->password = $this->rand_passwd(6); 
     $user->estado = true; 
     $user->persona_id = $persona->id; 
     $user->role_id = ADMIN; 

     if($this->Users->save($user)){ 

      $this->Flash->success('The administrador has been saved.'); 

      return $this->redirect(['action' => 'index']); 

     }else{ 
      $this->Flash->error('The administrador could not be saved. Please, try again.'); 
     } 

    } 
    $personas = $this->Administradors->Personas->find('list', ['limit' => 200]); 
    $this->set(compact('administrador', 'personas')); 
    $this->set('_serialize', ['administrador']); 

    $this->loadModel('Regions'); 
    $comunas = $this->Regions->Comunas->find('list', ['limit' => 200]); 
    $this->set(compact('comuna', 'comunas')); 
    $this->set('_serialize', ['comuna']); 
} 

的数据被插入到数据的基础,但闪存消息给我一个错误,对不起我的可怜的工程师中国语和THX

+0

看起来你没有加载'Flash'组件。在'initialize()'方法的'AppController.php'中,输入'$ this-> loadComponent('Flash');' –

+0

再次感谢Andre,但是在'AppController.php'中有'loadComponent('Flash' )',但我在方法'initialize()'的AdministradorsController中再次使用'loadComponent('Flash')',并且这次使用Flash Message。我不知道我为此做了什么。 –

+0

Edgar,在另一个控制器中再次加载'Flash'组件应该不成问题。你是否像这样扩展了'AppController':'类AdministradorsController扩展了AppController'。另一个可能导致问题的问题是,如果你在你的'AdministratorController'中定义'initialize()',而不是调用'parent :: initialize()'。 –

回答

3

如果您加载的Flash组件在AppController,这个问题可能是你在你AdministratorsController方法initialize(),你是不是在调用该方法parent::initialize();。它是必须的,因此所有的helpers,components等等,都是在AppControllerinitialize()方法中被初始化的。

0

缓解工作与Flash的消息,你可以用一个简单的通知插件turbo-tribble

1

在控制器:

<?php 
 
    $this->Flash->error(__('Your error message'), ['key' => 'error']); 
 
    ?>

在View:

<?= $this->Flash->render('error'); ?>

的src /模板/元/闪光灯/ success.ctp股利类更改为类= “警戒警报,成功”

<?php 
 
if (!isset($params['escape']) || $params['escape'] !== false) { 
 
    $message = h($message); 
 
} 
 
?> 
 
<div class="alert alert-success" onclick="this.classList.add('hidden')"><?= $message ?></div>

应用同类到src/Template/Element/Flash/error.ctp