Simple Models

Simple models are used for storing unique data and differ from standard ones by the following attributes:

  1. The class of model is inherited from "Model_Simple" class.
  2. SQL table of model contains only 2 "key" and "value" fields, where "key" - string primary key, "value" - a text field (it is possible to use string data type if a multiline text in field values isn't supposed to be used).
  3. When creating a model it is not necessary to insert the value of keys into table, in MV framework it is done automatically.

For the rest the creation of simple model is subject to the principles described General principles of models section.

Sample 1

<? 
class Seo extends Model_Simple
{
   protected $name = "SEO parameters";

   protected $model_elements = array(
      array("Title", "char", "title"),
      array("Keywords", "text", "keywords"),
      array("Description", "text", "description"),
      array("Robots.txt", "text", "robots")
   );
}
?>
CREATE TABLE `seo` (
  `key` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Sample 2

<?
class Site_Params extends Model_Simple
{
    protected $name = "Website parameters";

    protected $model_elements = array(
        array("Show a banner", "bool", "show_banner"),
        array("Slogan on main page", "char", "slogan"),
        array("Website Messages recipient", "email", "email", array("required" => true)),
        array("News number on main page", "int", "news_number", array("required" => true, 
                                                                      "positive" => true, 
                                                                      "zero_allowed" => false)),
        array("Footer text", "text", "footer_text", array("rich_text" => true))
    );
}
?>
CREATE TABLE `site_params` (
  `key` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Sample 3

<?
class Settings extends Model_Simple
{
    protected $name = "Project options";

    protected $model_elements = array(
        array("Show a banner", "enum", "promo", array("empty_value" => "Always", 
                                                      "values_list" => array("morning" => "In the morning", 
                                                                             "evening" => "In the evening", 
                                                                             "night" => "At night"))),
        array("Application date", "date_time", "work_date"),
        array("Price list", "file", "price_file"),
        array("Top image", "image", "top_image"),
        array("Gallery", "multi_images", "gallery")
    );
}
?>
CREATE TABLE `settings` (
  `key` varchar(100) NOT NULL,
  `value` text,
  PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Data retrieval process into templates for simple models is described at Data Display in Templates section.