Simple models
Simple models are used for storing unique data and differ from standard ones by the following attributes:
- The class of model is inherited from "Model_Simple" class.
- 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 multi line text in field values are not supposed to be used).
- When creating a model it is not necessary to insert the value of keys into table, in MV framework it is done automatically. You need only to create the SQL table.
All other principles of simple models are similar to regular models and described in 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;
Output of data from simple models into templates is described in Data display in templates section.
Previous section
Model settings