Cakephp 3中的Flash消息不起作用 [英] Flash Message in cakephp 3 it doesn't working

查看:76
本文介绍了Cakephp 3中的Flash消息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个简单的问题,我是蛋糕新手。

I'm new with cake sorry if this is a simple problem.

当我保存完数据并尝试显示一条有关if的信息时,用户已保存或无法保存,请显示下一个错误:

When i finish to save the data and i try to show a message with information about if the user is saved or could not saved , show me the next error :

错误:在非对象
文件上调用成员函数error() C:wamp\www\proyecto\src\Controller\AdministradorsController.php
行:76

Error: Call to a member function error() on a non-object File C:\wamp\www\proyecto\src\Controller\AdministradorsController.php Line: 76


AdministradorsController扩展了AppController

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']);
}

数据正在插入数据库,但Flash消息给我一个错误,对不起,我英语和英语不好

The data is inserting into the data base but the Flash message give me a error, sorry for my poor english and thx

推荐答案

如果您要加载 Flash AppController 中的c>组件,问题可能是您的<$ c中具有方法 initialize() $ c> AdministratorsController ,并且您没有在该方法中调用 parent :: initialize(); 。它必须正常工作,以便所有在<$ c中初始化的 helpers components 等。 $ c> AppController 的 initialize()方法已转移。

If you are loading the Flash component in your AppController, the issue could be that you have the method initialize() in your AdministratorsController and you are not calling parent::initialize(); in that method. It is a must for it to work so all the helpers, components, etc, that are initialized in AppControllers initialize() method are transferred over.

这篇关于Cakephp 3中的Flash消息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆