Frage

I'm trying to send a specific message from Model in beforeSave() method. Flash messages don't work. I could send this message from Controller and use some parameters but I don't this this best solution. Use of print isn't good either.

So my question is how to send any message to controller/view from model?

War es hilfreich?

Lösung

You have to bubble an error message, try this

in your model :

public function beforeSave($options = array()){
    if($not_ok){
        $this->error = __('My error message');
        return false;
    }
    return true;
}

in your controller :

public function add(){
    $save = $this->Modelname->save($this->request->data);
    if(!$save){
        $this->Session->setFlash($this->Modelname->error);
        $this->redirect($this->referer());
    }
}

Andere Tipps

Well Session->setFlash() will not work, obviously, as it's part of a Session component, but Session component uses static singleton class CakeSession, which has method CakeSession::write() all you've to do is pass array to write method that would have same structure as Session::setFlash() would generate and therefore when you use Session::flash() in view you will get same result as from setFlash() from controller.

For refrence: http://api.cakephp.org/2.2/class-CakeSession.html

Snippet from comment, to be placed in Model method.

App::uses('CakeSession','Model/Datasource');            
            CakeSession::write('Message', array(
                'flash' => array(
                    'message' => 'your message here',
                    'element' => 'default',
                    'params' => null,
                ),
            ));

By doing the following you will be able to set flashes within your models at any point and not have to worry about declaring them again within your controller because they will get automatically set within your app controller before the page is rendered.

In AppController:

public function beforeRender() {
  parent::beforeRender();
  $this->generateFlashes();
}

public function generateFlashes() {
  $flashTypes = array('alert', 'error', 'info', 'success', 'warning');
  $model = $this->modelClass;

  foreach($flashTypes as $type) {
    if(!empty($this->$model->$type)) {
      $message = '<strong>' . ucfirst($type) . ':</strong> ' . $this->$model->$type;
      $this->Flash->error($message, array('escape' => false));
    }
  }
}

In Model:

public function beforeSave($options = array()){
  if($not_ok){
    $this->error = __('My error message');
    return false;
  }
  return true;
}

In Controller:

public function add(){
  // $this->modelClass will use whatever the actual model class is for this
  // controller without having to type it out or replace the word modelClass
  $save = $this->{$this->modelClass}->save($this->request->data);
  if(!save){
    // no need to set flash because it will get created in AppController beforeRender()
    $this->redirect($this->referer());
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top