1
votes

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.

2
Model class names should be singular. Do debug(get_class($Weather)); it will most likely be AppModel and not Weathers as you have it now. - tigrang
Thanks, I've tried and it doesn't help. And the result for debug is AppModel - Vahan
Change the class name to Weather and the file name to Weather.php. What happens is if cake can't find your model, it creates one based off of AppModel - tigrang
Yes, the problem was with filename. I appreciate your help. - Vahan

2 Answers

1
votes

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

1
votes

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.