Simple model management

Simple models are based on the same data types as regular models, but they have different structure, because keep unique data elements. Their data is organized as key-value pairs stored in database, so their methods for getting and setting values have their own characteristics.

  • setValue($field, $value) - to set field value for further use or update.
  • getEnumTitle($field) - to receive the name (not a key!) of a field of "enum" type.
  • update() - update of 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 be returned.

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 fields into separate 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 update 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");