Files
piratepoet/lib/Models/Setting.php
marco ce730d7c3b Settings router.
Here's a simple settings router. the createOrUpdate method lives
in the model, and by default all models will return true on save
or false if save went wrong.
2015-08-19 23:55:30 +02:00

36 lines
843 B
PHP

<?php
namespace MailPoet\Models;
if (!defined('ABSPATH')) exit;
class Setting extends Model {
public static $_table = MP_SETTINGS_TABLE;
function __construct() {
parent::__construct();
$this->addValidations("name", array(
"required" => "name_is_blank",
"isString" => "name_is_not_string"
));
$this->addValidations("value", array(
"required" => "value_is_blank",
"isString" => "value_is_not_string"
));
}
public static function createOrUpdate($model) {
$exists = Setting::where('name', $model['name'])
->find_one();
if($exists === false) {
$new_model = Setting::create();
$new_model->hydrate($model);
return $new_model->save();
}
$exists->value = $model['value'];
return $exists->save();
}
}