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 enables access and values update of record fields in a table. Query parameters "$params" are an associative array described in Query Constructor section.

For example, take 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 a method "display()" we will see a lint of links on the book page. 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 on it's 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 $book -> date; ?></p>
        <? echo $book -> resizeImage("photo", 200, 300); ?>
        <h4>Short description</h4>
        <? echo $book -> description; ?>
   </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 work

Access to the fields of extracted records is done as an access to the object properties (if the property exists in the object then it’s 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) -to get the name (not an id) of "enum" field


The object of the class Record has methods for operation with files and images as described in the Files and images section.

  • resizeImage($field, $width, $height) - resize the image with keeping proportions
  • cropImage($field, $width, $height) - resize and crop the image
  • displayImage($field) - display HTML tag img for an "image" field
  • displayFileLink($field) - display a link to download file
  • extractImages($field, [$no-comments]) - extract images of "multi_images". Data of multi_images type are 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 to separate the 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 field of "multi_images" type
  • getFirstImage($field) - extraction of the first image from a "multi_images" field
  • wrapInParagraphs($field) - breaks the text from textarea into paragraphs

Also in the class Record there are "create()", "update()" and "delete()" methods as described in the Records Managements section.