0
public function upload ($data = null) {
    foreach ($data as $file) {
        $filename = $file['name'];
        $file_tmp_name = $file['tmp_name'];
        $dir = WWW_ROOT.'img'.DS.'uploads';
        $allowed = array('png', 'jpg', 'jpeg');

        if (!in_array(substr(strrchr($filename, '.'),1) , $allowed)) {
            throw new NotFoundException("Error Processing Request", 1);
        }
        elseif (is_uploaded_file($file_tmp_name)) {
            $new_filename = DS.STring::uuid().'-'.$filename;
            move_uploaded_file($file_tmp_name,$dir.$new_filename);

            if ($this->Upload->save($this->request->data)) {
            `$this->Session->setFlash('Data Saved!');
            }
        }
    }
}

And I got this error..

Call to a member function save() on a non-object

How to can I fix this?

4 Answers4

1
$model = ClassRegistry::init('Upload');

then use

 $model->save();

use

if (ClassRegistry::init('Upload')->save($this->request->data)) {

            }
Vivek S
  • 2,010
  • 1
  • 13
  • 15
1

Why didn't you pluralized the name of the controller and its filename (and also the table name), i.e. UploadsControllerl? It's a CakePHP convention:

Controller class names are plural, CamelCased, and end in Controller. PeopleController and LatestArticlesController are both examples of conventional controller names.

Edit: I thought your method was in a controller class. In a component, before using a model you need to register it as follows:

$model = ClassRegistry::init('Yourmodel');

so you should use:

$model = ClassRegistry::init('Upload');
$model->save($this->request->data);

see: Use a model in a component

Community
  • 1
  • 1
Mohsenme
  • 1,012
  • 10
  • 20
1

use ClassRegistry if you are using model in component or in other modles if it has no association. use

ClassRegistry::init('Upload');
mano_msk
  • 51
  • 1
  • 9
0

Please load the model in your controller function suppose you are work with users table then you load the User model in the function $this->loadmodel('User'); after then you can use the code

if($this->User->save($this->request->data)){
     your code......

}

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46