Object of Record class
A table record can be extracted using model methods "findRecord($params)" and "findRecordById($id)", that return the object of Record class. Object of this class can access the values and update data in SQL table. Query parameters "$params" are an associative array described in Query constructor section.
For example, we have a model that describes books.
class Books extends Model { protected $name = "Books"; protected $model_elements = array( array("Name", "char", "name", array("required" => true)), array("Author", "enum", "author", array("foreign_key" => "Authors")), array("Publsih date", "date", "date"), array("Cover image", "image", "photo"), array("Brief description", "text", "description") ); public function display() { //All records in alphabet order $rows = $this -> select(array("order->desc" => "name")); $html = ""; foreach($rows as $row) // create a link to each book $html .= '<a href="'.$this -> root_path.'book/'.$row['id'].'/">'.$row['name'].'</a>'; return $html; } }
After calling of a method "display()" we will see a list of links referring to the books pages. Create a template "view-book.php" file to display information about a book. In "config/routes.php" add a line "book/*/" => "view-book.php'".
The content of "view-book.php" file.
<? //Take the second part of URL and check if it is numeric $book_id = $mv -> checkUrlPart(1, "numeric"); //Extract a record by its id $book = $mv -> books -> findRecordById($book_id); //If record not found, show 404 error $mv -> display404($service); include $mv -> views_path."main-header.php"; ?> <div id="content"> <h2><? echo $book -> name; ?></h2> <p>Author: <? echo $book -> getEnumTitle("author"); ?></p> <p>Date: <? echo I18n :: formatDate($book -> date); ?></p> <? echo $book -> resizeImage("photo", 200, 300); ?> <h4>Short description</h4> <p><? echo $book -> description; ?></p> </div> <? include $mv -> views_path."main-footer.php"; ?>
The same way it is possible to get the newest book from a list or the first book of the specific author.
$book = $mv -> books -> findRecord(array("order->desc" => "date")); $book = $mv -> books -> findRecord(array("order->asc" => "date", "author" => 42));
Principles of Record object
Access to the fields of extracted records is done as an access to the object properties (if the property exists in the object then its value will be returned).
//Get the property echo $book -> name; echo I18n :: dateFromSQL($book -> date); echo nl2br($book -> description); //Set the value of property $book -> name = "The Catcher in the Rye"; $book -> description = "American classic"; $book -> author = 32; //Id of "Salinger" record in "Authors" model table //Extract the name (title, not an id) of the field of "enum" type echo $book -> getEnumTitle("author");
Other methods
- setValues($values) - pass an array of field values in object of Record
- getValues() - to extract values of all fields in the form of an associative array
- passContent($content) - passes data from a source (should be one-dimensional associative array and to have the id field)
- getEnumTitle($field) -t o get the name (not an id) of "enum" field
The object of the class Record has methods for operations with files and images as described in the Files and images section.
- resizeImage($field, $width, $height) - resize the image with kept proportions
- cropImage($field, $width, $height) - resize and crop the image (proportions may be changed)
- displayImage($field) - display HTML tag "img" for an "image" type field
- displayFileLink($field) - display a link to download a file
- extractImages($field, [$no-comments]) - extract images of "multi_images". Data of multi_images type is stored in a database as one fragment of the text which is a path to images on a hard drive. Paths are concatenated by a separator, also in the end of the path to the file there can be a comment to this image. When extracting data of this type from database you need to separate images and comments from them.
Suppose in model there is a field of "multi_images" type with names "pictures", and $product - is a sample of class "Record".
//Associative array (keys - paths to images, values - comments to images accordingly) $images = $product -> extractImages("pictures"); //Standard array of paths to images (without comments) $images = $product -> extractImages("pictures", "no-comments");
- combineImages($field, $images) - packing of images in the format of field of "multi_images" type
- getFirstImage($field) - extraction of the first image from a "multi_images" field without comment
- wrapInParagraphs($field) - breaks the text from textarea into paragraphs
Also in the class Record there are "create()", "update()" and "delete()" methods as described in records managements section.
Previous section
Data display in template