1

I've got a method in UsersController

    public function addMailbox($data)
    {
              $this->LoadModel('Mailbox');
              $mailbox = $this->Mailbox->newEntity();
              $mailbox->username = $data('username');
              $mailbox->name = $data('name');

        if ($this->Mailbox->save($mailbox)) {
            return $this->redirect(['action' => 'index']);
            }
        $this->Flash->error(__('Error'));
       }

, the code works fine when pasted into the add() method, but after using

     $this->addMailbox($this->request->getData());

all i get is error: Function name must be a string

Any ideas?

1 Answers1

2

You've got the wrong syntax for accessing arrays in PHP, use square brackets:

$mailbox->username = $data['username'];
$mailbox->name = $data['name'];

The way you've got it, it's trying to call a function with the variable named in $data, but $data is an array not a string (see Variable functions for more info on that).

Also you shouldn't set user input on $mailbox properties directly - this bypasses validation. Instead just stick $data in newEntity():

public function addMailbox($data)
{
    $this->loadModel('Mailbox'); // This also is not required if this function is inside the MailboxController
    $mailbox = $this->Mailbox->newEntity($data);

    if ($this->Mailbox->save($mailbox)) {
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('Error'));
}
Andy Hoffner
  • 3,267
  • 2
  • 21
  • 22