Simple Model management

  • setValue($field, $value) - to set field value for further use or updating.
  • getEnumTitle($field) - to receive the name (not a key!) of a field of "enum" type.
  • update() - update data of simple model (without saving a new version).
  • increaseCounter($field, [$value]) - increase counter value by 1, if the optional argument has been passed, the value will be increased by the value of argument. To create a counter, add an integer field into Simple Model, where the value of the counter will be stored further.
  • decreaseCounter($field, [$value]) - decrease the value of the counter by 1, if the optional argument has been passed, value will be decreased by the value of argument.
  • combineEmails([$fields]) - takes all "email" fields and concatenates them with "," character, in the optional parameter it is possible to pass the names of the fields that need to be returned as an array, if no argument passed, all filled fields of email type will return.

Attention! The call of the "update()", "decreaseCounter()" and "combineEmails()" methods won't lead to creation of the version in the history of changes.


//Create a Simple Model
class Options extends Model_Simple
    protected $name = "Settings";
    protected $model_elements = array(
        array("Show a banner", "bool", "banner_active"),
        array("Email of admin", "email","admin-email", array("required" => true)),      
        array("Email of admin (orders)", "email", "admin-email-2"),
        array("Email of admin (feedback)", "email", "admin-email-3"),
        array("Phone", "phone", "phone"),
        array("Downloads counter", "int", "counter"),
        array("Action of the day", "enum", "promo", array("values_list" => array("discount" => " 10% discount", 
                                                                                 "tickets" => "Concert tickets", 
                                                                                 "gift" => "Special gift"))),
        array("Ad text", "text", "text"),
        array("Price list ", "file", "price"),
        array("Banner", "image", "banner"),
        array("Image gallery", "multi_images", "gallery")
    protected $model_display_params = array( 
        //Close counter for editing and divide into tabs
        "not_editable_fields" => array("counter"),
        "fields_groups" => array("Main settings" => array("banner_active", "admin-email", "admin-email-2", 
                                                          "admin-email-3", "phone", "counter", 
                                                          "promo", "price", "banner"),
        "Текст" => array("text"),
        "Gallery" => array("gallery"))
//Data extraction
echo $mv -> options -> phone;
echo $mv -> options -> getEnumTitle("promo");

$record = $mv -> options -> loadIntoRecord();
echo $record -> text;

if($record -> banner_active)
    echo $record -> cropImage("banner", 200, 200);
echo $record -> displayFileLink("price", "Download file");

//Data Updae of Simple Model
$mv -> options -> setValue("admin-email-2", "") -> setValue("text", "Test message!");
$mv -> options -> update();

//Increase/decrease counters 
$mv -> options -> increaseCounter("counter");
$mv -> options -> increaseCounter("counter", 3);

$mv -> options -> decreaseCounter("counter");
$mv -> options -> decreaseCounter("counter", 8);

//Creating a list of emails
$recipients = $mv -> options -> combineEmails();
Email :: send($recipients, "A message from the website", "Message text");