How to Get October CMS Tailor Collection via PHP

Category: Guides, October CMS
Last modified on December 13, 2024

Introduction

When creating custom entity types in October CMS, the built-in Tailor feature offers a convenient solution. You no longer need to directly edit PHP files or create plugins to manually declare database tables. However, the Tailor system has some limitations when it comes to advanced record management.

Let’s imagine we want to change the sorting or add more complex logic when retrieving records. These features are not available in the standard Tailor collection component. In this case, we can use the EntryRecord model. Let’s see how to do it.

Suppose we have a Blueprint that creates a record type with the following parameters:

handle: Blog\Post name: Blog Post fields: title: label: Title type: text content: label: Content type: textarea

To retrieve entries on the front end, we need to use the handle parameter value from our Blueprint.

Retrieving Entries

To retrieve the entries in the page or layout template, first open the PHP section and add the following code inside the onStart() function. Define the use of the Tailor\Models\EntryRecord class, and use the static inSection() method with an argument matching the handle value from our Blueprint.

The get() method retrieves records from the database based on the given configuration.

function onStart () { use Tailor\Models\EntryRecord; $this[ 'posts' ] = EntryRecord::inSection( 'Blog\Post' )->get(); }

If you want to get a specific number of records, use the take() method. For example, let’s retrieve the last three records:

$this[ 'posts' ] = EntryRecord::inSection( 'Blog\Post' )->take( 3 )->get();

You can also filter records based on a specific column value:

$this[ 'posts' ] = EntryRecord::inSection( 'Blog\Post' ) ->where( 'custom_field', 'my_value' ) ->get();

Useful Resources

If you want to dive deeper into working with October CMS Tailor or enhance your understanding of custom entities and models, here are some helpful links: