Models are used to interact with the database and structure data that you are using in your Fabriq based website or application. Modules can be used in one of two ways: 1) structure data that you are working with in your application; 2) to map to a table in a database.
Models should follow proper PHP class naming conventions. All models extend the core model class Model in /core/Model.class.php. You should give the model class a name that is relevant to the data that it stores. The name of the model should also be plural because it represents a collection (even if there is only one object stored by the model, it is a collection of one object). All models should be saved in the format ModelName.model.php in the /app/models/ directory of the website or application.
Models can contain multiple methods as well as the methods provided by the core Model class in /core/Model.class.php.
The model constructor calls the parent Model class' constructor. The Model class' constructor takes a required array of strings naming the attributes of the model. For models that map to a database table, a second parameter that is the name of the database table. Also for models that map to a database table, an optional third parameter that names the ID field of the database table may be provided if the table's ID field has a name that is different than id.
The base Model class contains the attributes $created and $updated. These fields are automatically set by Fabriq whenever you first create your object and insert into the database and the updated attributed is updated anytime you update the row in the database. When using a model that maps to a database table, you need include these fields as data type DATETIME in your table definition. These fields can be helpful for tracking down when information was changed as well as automatically setting a creation date for the data.
A Model collection contains properties and functionality of arrays. A collection can be iterated over using loops and a specific item in the collection can be accessed by it's index in the collection (ex: $collection[5]). If no index is provided, the first item in the collection is used.
Structuring Data
The model class can be used to structure data that is used in your website or application. If you have several related objects that are of the same structure, you can create a model to use a collection.
Example:
class Tests extends Model { public function __construct() { parent::__construct(array('var1', 'var2', 'var3')); } }
Example instantiating and adding data to a collection based on the example class above:
$test = new Tests(); $t = new stdClass(); $t->var1 = 'Hello'; $t->var2 = 'World'; $t->var3 = 112358; $test->add($t);
Map to database table
The model class can be used to map an object structure to a table in a database. As a suggestion, it is helpful to shortcut the constructor to accept an optional ID that if given will automatically select the object with that ID from the database. When defining a model that maps to a database table, you must provide a second parameter that is the name of the database table (by convention, the name of the model should match the name of the database table).
Example:
class Test2s extends Model { public function __construct($id = NULL) { parent::__construct(array('field1', 'field2', 'field3'), 'test2s'); if ($id != NULL) { $this->find($id); } } }
Example instantiating a collection with a given ID.
$test = new Test2s(15); // display a field from the collection echo $test->field2;
Read the documentation for the core Model or view the code to get an idea for how to create and work with models in Fabriq.
