Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
Given are the following models (adapt the class names and namespaces to your usecase): * Post: This is the base model, a simple blog post. We want to add the relation manager to this model's form. * Comment: This is the related model. A blog post has many comments. ```php <?php namespace Acme\Blog\Models; class Post extends Model { // The relation manager works for any relation type, use whatever you need. public $hasMany = ['comments' => \Acme\Blog\Models\Comment::class]; } ``` ## Implementing and configuring the relation controller In your `PostsController` add the following lines: ```php <?php namespace Acme\Blog\Controllers; class Posts extends Controller { public $implement = [ 'Backend.Behaviors.FormController', 'Backend.Behaviors.RelationController', // Add this! ]; public $formConfig = 'config_form.yaml'; public $relationConfig = 'config_relation.yaml'; // Add this! } ``` Next, create the `config_relation.yaml` file in `plugins/acme/blog/controllers/posts/config_relation.yaml`. Paste the contents below (in case of a `hasMany` relation) into the yaml file. You can find more examples for different relationship types (`hasOne`, `belongsToMany`, etc) <a href="https://octobercms.com/docs/backend/relations#relationship-types" target="_blank"> in the official docs</a>. ```yaml comments: label: Comment deferredBinding: true view: list: $/acme/blog/models/comment/columns.yaml toolbarButtons: create|delete manage: form: $/acme/blog/models/comment/fields.yaml recordsPerPage: 10 ``` ## Displaying the relation manager Create a new file in `plugins/acme/blog/controllers/posts/_comments.htm`. Paste the following contents: ```php <?= $this->relationRender('comments'); ?> ``` The `comments` key corresponds to the base key in your `config_relation.yaml`. Now you can include this partial in your model's form definition to display the relation manager. ```yaml # in plugins/acme/blog/models/post/fields.yaml comments: type: partial path: comments # includes _comments.htm from the current controller's path.
References
Add additional online resources to your trick
×
Name
URL
×
Name
URL
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline