iwj145
iwj145

Reputation: 261

How to find out where my functions errors are (CAKEPHP)

I'm having trouble working out where I went wrong in my function and I would like to know if there was anyway for me to find out where the function breaks either using net beans or whilst I'm executing the function.

If you would like to see the function then here's a copy of it:

             public function archive($id = null) {
    if ($this->request->is('post')) {
        $event =  $this->Event->read(null, $id);
        $archive['Archive'] = $event['Event'];
        $archive['Archive']['archiveID'] = $event['Event']['eventID'];
        unset($archive['Archive']['archiveID']);
        $this->loadModel('Archive');
        $this->Archive->create();
        if ($this->Archive->save($archive)) {
           // $this->Archive->invalidFields();
            $this->redirect(array('action' => 'eventmanage'));
            $this->Session->setFlash(__('The event has been archived'));
          //  $this->Event->delete($id);  
        } else {
            $this->redirect(array('action' => 'eventmanage'));
            $this->Session->setFlash(__('The event could not be archived, Please, contact the administrator.'));
        }
    }
}

so far i have tried using the `$errors = $this->Archive->validationErrors;` function but all i receive back is the word 'array' 

UPDATE

I have got the event to save as an archive, however it does not send the message $this->Session->setFlash(__('The event has been archived')); And it does not redirect back to eventmanage, it creates a new link to the /Controller/Action/id e.g. /events/archive/14 .

Thanks

Upvotes: 0

Views: 111

Answers (2)

Oldskool
Oldskool

Reputation: 34867

The validationErrors property returns an array. You can't echo an array object in PHP, as it's a collection of data, rather than something that can "just" be output to your screen. There are other functions to display the full array though, like var_dump(), print_r() or the CakePHP methods debug() or Debugger::dump(). So try something like this:

$errors = $this->Archive->validationErrors;
if (!empty($errors)) { debug($errors); }

You can also use implode() to output all the validation errors as a string in your flash message, for example:

$this->Session->setFlash(__('The event could not be archived, because of the following errors: ' . implode('<br/>', $errors) . '. Please, contact the administrator.'));

That will display all the errors, separated with a break within the flash message.

Update In regard to your latest question update, the flash message is not set, because you redirect before you set it. So, the statement is never reached. Swap the setFlash and redirect methods in your controller, so you'll get:

$this->Session->setFlash(__('The event has been archived'));
$this->redirect(array('action' => 'eventmanage'));

Upvotes: 4

AKKAweb
AKKAweb

Reputation: 3807

I am having a hard time understanding the need to assign a value to

$archive['Archive']['archiveID'] = $event['Event']['eventID'];

and unsetting it right after

unset($archive['Archive']['archiveID']);

If in your Db 'archiveID' is a required value, then I dont think MySQL would accept any data if that value is not there.

You would have to check the structure your table.

Upvotes: 0

Related Questions