Model syntax & usage
- Defining
- Available datatypes
- many-to-many
- many-to-one
- one-to-many
- one-to-one
- Single object
- Multiple objects
- Complex/relational queries
Model methods & vars
Static methods
Instance methods
Instance variables
Magic methods
Model syntax & usage > Basic syntax
A model is the single, definitive description of your data. It is a class, from which individual objects will be created. It contains the fields and behaviors of the data you are storing. Each model maps to a single database table, and each object is one row in that table.The basics:
- database fields are defined in the __model() method
- the model will be used to create, access, and search for stored data
- the model is stored in a mysql table, each object is one row
- each model is an extension of zajModel and thus has access to its methods and fields
A quick example
Let's create a Band class! This very simple version of Band will have a name, a genre (the style of music), and a biography.
class Band extends zajModel {
/**
* __model function. creates the database fields available for objects of this class.
*
*/
static function __model(){
/////////////////////////////////////////
// begin custom fields definition:
$f->name = zajDb::name();
$f->genre = zajDb::text();
$f->biography = zajDb::textarea();
// end of custom fields definition
/////////////////////////////////////////
// do not modify the line below!
$f = parent::__model(__CLASS__, $f); return $f;
}
/**
* Contruction and static calling methods. These are required and not to be modified!
*/
public function __construct($id = ""){ parent::__construct($id, __CLASS__); return true; }
public static function __callStatic($name, $arguments){ array_unshift($arguments, __CLASS__); return call_user_func_array(array('parent', $name), $arguments); }
}
The Model file above creates a database table with the desired columns (name, genre, biography) added in addition to some default columns (id, ordernum, time_create, time_edit, status). Thus the resulting table will look like this:
CREATE TABLE `band` (
`id` varchar(50) NOT NULL COMMENT 'object id',
`name` varchar(100) NOT NULL,
`genre` varchar(100) NOT NULL,
`biography` text NOT NULL,
`time_create` int(11) NOT NULL,
`time_edit` int(11) NOT NULL,
`ordernum` int(11) NOT NULL,
`status` enum('new','deleted') NOT NULL DEFAULT 'new'
)
Creating models
To add a new model, you must create a new file in the /app/model/ folder of your Mozajik application. The file must be named according to the name of the model, so the above Band model's file would be called band.model.php. Its contents must include the __model(), __construct(), and __callStatic() methods.After adding, modifying, or removing and model fields, you must run an application model update. Mozajik will now automatically update the database according to your model field definitions.
So, to summarize:
- model files must be in /app/model/ folder of your Mozajik application
- they must be named "modelname.model.php" (all lowercase)
- the model class name should be camel-case - "ModelName"
- you must run a model update after adding or modifying any of your model fields. this is done from the Mozajik update menu (ex: http://yourmozajikapp.com/update/)
