Models creation
Models in lattenoir map table rows and allow you to work with them through an ORM interface.
Among other things, the models also support the creation of automatic admin panels and working with localized fields.
Internally, model is built into a set of SQL, JS and HTML files to be bundled with your website.
Pre-requisites: creating table in PostgreSQL
To create a new model, you should create its corresponding database table first.
If there are not localizable fields in table, just create it.
-- (for some bar menu)
create table Tbar_categories (
id bigserial primary key,
parent_id bigint references Tbar_categories, -- parent category
enabled integer not null, -- show category or hide it
image_id bigint references Tfiles, -- link to photo
name varchar(128), -- category name
description text -- category description
);
If there are fields which are meant to be localized, you need to create two tables, first one does NOT contain any localizied fields (we shall call it main table here), whereas localization one contains only localized fields, plus references to languages table and to main table. Localization table name must be (main table name)+ "_l10n" and must contain fields fid_id and l10n_id.
create table Tbar_categories (
id bigserial primary key,
parent_id bigint references Tbar_categories,
enabled integer not null,
image_id bigint references Tfiles
);
create table Tbar_categories_l10n (
id bigserial primary key,
fid_id bigint not null references Tbar_categories,
l10n_id bigint not null references Tl10n,
name varchar(128),
description text
);
-- create unique index on pair (category,language)
create unique index Ibar_categories_l10n on Tbar_categories_l10n (fid_id,l10n_id);
Since for every main table entry there can be only one translation per language, it is highly recommended to create unique index like following:
create unique index Ibar_categories_l10n on Tbar_categories_l10n (fid_id,l10n_id);
Adding entry to "Model Generator"
Recommended naming conventions:
Unit | Example | Description |
---|---|---|
Model | BarCategory | CamelCase in singular |
Database table | Tbar_categories | Table name in plural, with prefix "T", meaning table |
Folder name | bar_categories | Same as table name but without leadning letter "T" |
In order to create a new model entry, go to the website admin panel bound to the url /admin/, navigate to "Model generator" section, press "Add new, using database table" and fill in the details according to conventions. To edit main details of the existing model, find it in the list and press "edit".
NOTE: After model addition or changing model configuration, always run "update" and "rebuild" for the changes to take effect.
Initial model setup
Advanced level of model editing is called tuning. It allows to set up fields of an object, set convenient pre-defined queries, configure generated editor UI and other things. To tune model instance, press "tune" button next to the specific model instance.
Auto-generated editor is called Admin Panel (A/P). Below you can see some examples of admin panel. It is the basic view that can be extended later.
All columns with their properties are listed in the "Database fields" section.
- Datatype is an intristic type of the column. It depends on type of column in database and on other conditions. Datatype is primarily used for data serialization/deserialization and in the adminpanel UI.
- Datatype can be overriden in column Change datatype.
- Skip allows not to include database column in model instance.
- A/P filter flag toggles