Vahan
Vahan

Reputation: 169

Using CakePHP model class

I'm just studying CakePHP so sorry for any obvious mistakes. I have created model class and changed default table name.

class Weathers extends AppModel {    
    public $tablePrefix = 'weather_';
    public $useTable = 'forecasts';

    function saveCountries($countries){
         ...
    }
}

And my controller function

if (!$this->loadModel('Weather'))
   exit;
$Weather = $this->Weather;
$Weather->saveCountries($countries);

I'm getting error on $Weather->saveCountries($countries);

Error: Table weathers for model Weather was not found in datasource default.

Please help find out what I do wrong.

Upvotes: 1

Views: 1145

Answers (2)

Predominant
Predominant

Reputation: 1460

Note the declaration of your Model class.

You've called it Weathers.

There is no problem with this. However, as you are trying to then load the model Weather (not the lack of plural in this case) CakePHP is constructing a model dynamically for you, instead of using your Weathers (plural) class.

The CakePHP standard is to use a singular name for models. I suggest that you rename your model class to Weather to avoid this issue. Once you make this change, the code that you have for loading the model will work as intended.

Upvotes: 1

Arun Jain
Arun Jain

Reputation: 5464

The Model class you defined is Weathers not Weather. So just change the class name Weather instead of Weathers and this is done.

Upvotes: 1

Related Questions