Records management
The Record class in MV framework manages the single line in the table of model. Record consists of fields of different types, the same as model, where the record came from. For operations with table records it is possible to use model itself or Record class object, that manages a one string of the table. Differences of these approaches are the following:
- When using the object of model, the version available in admin panel, will be saved.
- Also, when using the object of model there is a check of a field on ability to edit records (tuning of model).
- When operating with an object of model the methods "beforeCreate()" and "afterCreate()" will be called.
- The object of the Record class doesn't save the versions and doesn't check fields 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 an option of recovery.
Attention! Methods, operating with records affect only the fields of database table which are declared in model in "$model_elements" array. Theoretically in the SQL table there can be additional fields, it's possible to work with them using Direct SQL queries to database.
Below we will show 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
//Begin with an empty 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
//Takes record from databas by its id $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
$story = $mv -> stories -> findRecord(array("name" => "How tocut the grass in fall")); //A record is deleted without moving it to bin $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); //Gets the record, loads values of all fields into model object if($mv -> stories -> checkRecordById($id)) $mv -> stories -> read($id); else ... //no such record //The same way as when creating a record, after that call update method $mv -> stories -> getDataFromArray(...); $mv -> stories -> update();
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, 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();
Deleting of several records at a time
Use "deleteManyRecords($conditions)" method, where $conditions โ query conditions, described in Query constructor section.
$mv -> stories -> deleteManyRecords(array("active" => 0, "theme" => "home"));
Deleting of all records
Method "clearTable()" deletes all records and drops "auto increment" index for SQL table. If first parameter was passed, so the table with such name is being cleared, instead of base table by default.
//Clearing of stories table $mv -> stories -> clearTable(); //Clearing of comments table $mv -> stories -> clearTable("comments");
Previous section
Group