3: Relations

The CMS models three relation types, all declared as fields and all derived automatically from the schema.

Type Meaning Example
parent belongs-to-one an article has one author
child has-many an author has many articles
many many-to-many an article has many topics

3.1: Parent and child

A parent field stores the related row's id and resolves to the related model when you read it:

// on article
author:   field (type: 'parent', obj: 'author',   title: 'Author', required: true)
category: field (type: 'parent', obj: 'category', title: 'Category')
// on author, the reverse side
articles: field (type: 'child', obj: 'article', title: 'Articles')

The column convention matters. The foreign-key column is named after the related model's short name, not <name>_id. So an article's author is stored in a column called author, and the author's child lookup reads articles.author. Name the parent field to match: author, not author_id.

3.2: Many-to-many

A many field uses a join table:

topics: field (type: 'many', obj: 'topic', table: 'article_topic', title: 'Topics')

The join table's columns default to the two short names: article_topic(article, topic). The CMS shows the related rows read-only in the list and record views and edits the set on the record page.

3.3: Reading relations

Once declared, relations are plain property reads: $article->author is the author model, $article->author->name its name, and child counts (12 Comments) appear automatically in list views. No join SQL in your app code.

We use essential cookies to make this site work. With your permission we also use analytics to improve the site.