phemellc / yii2-settings

Yii2 Settings Module
Other
151 stars 74 forks source link

Settings cache problem #57

Closed dub34 closed 8 years ago

dub34 commented 8 years ago

Hi. I install your module. Add action to my settings controller. Create model and view for settings interface. If i submit form, all settings save to DB and all work fine, but if i clear cache, all fileds in view are empty. But in DB it`s already exist. Can you help to fix it? Thank you for your work. Module is great.

arisk commented 8 years ago

Hi. Can you post the relevant code for your model and controller? In many cases you may not need to clear the cache manually as it's done in the model's afterSave and afterDelete methods. You're welcome by the way :)

dub34 commented 8 years ago

use backend\models\Settings; use pheme\settings\SettingsAction; class SettingsController extends \yii\web\Controller {

public function actions()
{
    return [
        'index' => [
            'class' => SettingsAction::className(),
            'modelClass' => Settings::className(),
            'viewName' => 'index'
        ]
    ];
}

}

class Settings extends Model {

public $system_tariffs; public $order_distance; .... public function formName() { return 'AdminSettings'; }

public function rules()
{
    return [
        [
            [
                'system_tariffs',
                'tariff_price_blocked_wheel',
                'tariff_price_blocked_steering_wheel',
                'tariff_price_truck_blocked_wheel',
                'tariff_price_truck_blocked_steering_wheel',
                'tariff_included_mileage',
                'urgent_order_time',
                'search_vehicle_timeout',
                'send_all_timeout',
                'vehicle_loading_timeout',
                'driver_app_version',
                'default_tariff_type',
                'agent_order_timeout',
                'intercity_service_commission',
                'auto_discount_time'
            ],
            'integer',
            'min' => 0
        ],
        [
            [
                'order_distance',
                'commission',
                'tariff_discount_website',
                'first_send_distance',
                'agent_order_commission',
                'agent_order_timeout',
                'sms_price'
            ],
            'double',
            'min' => 0
        ],
        ['credit_card_commission', 'double', 'min' => 0, 'max' => 100],
        [
            ['first_send_distance', 'send_all_timeout', 'vehicle_loading_timeout', 'driver_app_version'],
            'default',
            'value' => 0
        ],
        [['disable_agent_orders', 'check_driver_app_version'], 'boolean'],
        [['service_phone'], 'string'],
    ];
}

}

I override Settings component class. And override method get to hardcode section param beacause i want to get on backend only backend settings, and on frontend only user settings. And i do it through section.

namespace common\components\settings; use backend\models\Settings as SettingsModel;

class Settings extends \pheme\settings\components\Settings { public function get($key, $section = null, $default = null) { return parent::get($key, (new SettingsModel)->formName(), $default); } }

arisk commented 8 years ago

You may want to configure the component's modelClass. By default it's pheme\settings\models\BaseSetting which is not what you seem to be using. If you do, don't forget to implement pheme\settings\models\SettingInterface. That should fix your problem.

dub34 commented 8 years ago

by default Settings component use model pheme\settings\models\Setting. I don`t override it. If you mean, i need to create my own model and hardcode section in it, but it not fix problem with settings view.

arisk commented 8 years ago

How are you clearing the cache? Since you're using your own controller and view for the UI it shouldn't be anything related to the component. Are there any errors in the your logs? Cache component can read and write correctly?

dub34 commented 8 years ago

Hi. I understand the problem. I have 2 projects with this module on 1 server with memcache. And both they use default cache key in settings. Thats why i get settings rawConfig from other project :( Set custom cache key to config and all work. Thanks