kuvalda-settings/Settings.php

90 lines
2.0 KiB
PHP

<?php
namespace dominion\settings;
use Yii;
/**
* This is the model class for table "settings".
*
* @property int $id
* @property string $code
* @property string $value
*/
class Settings extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'settings';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['code'], 'required'],
[['code'], 'string', 'max' => 500],
[['value'], 'string', 'max' => 5000],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'code' => 'Code',
'value' => 'Value',
];
}
public static function getData($params, $root)
{
$query = self::find();
$root->andWhere($query, $params, ['id', 'code']);
$root->andWhereLike($query, $params, ['value']);
$root->setMetaByQuery($query);
$root->orderBy($query, $params, ['id', 'code', 'value']);
return $root->getItemsByPage($query);
}
/**
* Получить настройки
*
* @param string $code символьный код уникальный
* @param string $defaulValue значение по умолчанию
* @return string
*/
public static function getValue($code, $defaulValue = false)
{
$model = Settings::find()->andWhere(['code' => $code])->one();
return empty($model) ? $defaulValue : $model->value ;
}
/**
* Сохранить настройк
*
* @param type $code
* @param type $value
*/
public static function setValue($code, $value)
{
$model = Settings::find()->andWhere(['code' => $code])->one();
if(empty($model))
{
$model = new Settings;
$model->code = $code;
}
$model->value = (string)$value;
return $model->save();
}
}