Records Management

The Record in MV frmework means the single line in the table of model. Record consists of fields of different types. For operations over table records it is possible to use model object or Record class object, that manages a one entry in the table. Differences of these approaches are the following:

  • When using the object of model, the version which is available in Admin Panel in the first column when editing record, will be saved.
  • Also, when using the object of model there is a check of a field on ability to edit records (Setup of model).
  • When operating in object of model the methods by "beforeCreate()", "afterCreate()" will be runed.
  • The object of the Record class doesn't save the versions and doesn't check a field for ability to edit.
  • When deleting through object of model the record will be moved into recycle bin, and when using Record class, it will be deleted without recovery.

Attention! methods on operation with records affect only on the firlds of Database table which are declared in model in "$model_elements" array. Theoretically in the table of a database there can be additional fields, it is possible to work with them using Direct SQL Queries to Database.

We will review records management on the example of the Model of stories which are added by users on the website.

class Stories extends Model
{
    protected $name = "Stories";

    protected $model_elements = array(
       array("Activate", "bool", "active"), array("Date", "date_time", "date", array("required" => true)),
       array("Name", "char", "name", array("required" => true)),
       array("Subject", "enum", "theme", array("values_list" => array("family" => "Family", 
                                                                      "home" => "House", 
                                                                      "travel" => "Travels"))),
       array("Content", "text", "content", array("required" => true))
    );
}

Use of Record Object

Creating a Record

$story = $mv -> stories -> getEmptyRecord();
$story -> active = 1;
$story -> name = "How to cut the grass";
$story -> theme = "home";
$story -> date = I18n :: getCurrentDateTime();
$story -> content = "Text";
$story -> create();

//Data for the fields can also be retrieved from an array
$story -> setValues($form -> getAllValues());
$id = $story -> create();

//"create()" method returns an id of created record,
//which can be used in the future

Editing a Record

$story = $mv -> stories -> findRecordById(75);
$story -> active = 0
$story -> name = "How to cut the grass in fall";
$story -> update();

//Also data can be retrieved from an array 
$story -> setValues($form -> getAllValues($fields));
$story -> setValues(array("active" => 1, "theme" => "family"));
$story -> update();

Deleting a Record

A record is deleted without moving it to bin.

$story = $mv -> stories -> findRecord(array("name" => "How tocut the grass in fall"));
$story -> delete();

Use of Model Methods

Creating a record

$form = new Form("Stories");
$fields = array("name", "theme", "content");

if(!empty($_POST)
{
    $form -> getDataFromPost() -> validate($fields);

    if($form -> isValid())
    {
        $mv -> stories -> getDataFromArray($form -> getAllValues($fields));
        $mv -> stories -> getDataFromArray(array("active" => 1, "date" => I18n :: getCurrentDateTime()));
        $new_id = $mv -> stories -> create();
    }
}

The method "$mv -> stories -> create()" returns an id of created record, that can be used further.

Editing a Record

The same as creating a record, just you need to have an id of the record and read the record from a database.

//we can receive an id of record from router, for example
$id = $mv -> router -> getUrlPart(1);

//Get the record, load values of all fields into model object
if($mv -> stories -> checkRecordById($id))
     $mv -> stories -> read($id);
else
    ... //no such a record

//The same way as creating a record, after which call update method 
$mv -> stories -> getDataFromArray(...);
$mv -> stories -> update();

Simultaneous Editing of several recods at a time

Use "updateManyRecords($params, $conditions)" method, where $params –are parameters that need to be changed for all records, $conditions – query conditions, as described in Query Constructor section.

$mv -> comments -> updateManyRecords(array("read" => 1), array("user" => 523, "read" => 0));

Deleting a Record

///The record will be moved into bin 
$id = $mv -> router -> getUrlPart(1);
$mv -> stories -> setId($id) -> delete();

Simultaneous Deleting of several recods at a time

Use "deleteManyRecords($conditions)" method, where $conditions – query conditions, as described in Query Constructor section.

$mv -> stories -> deleteManyRecords(array("active" => 0, "theme" => "home"));