silverstripe / silverstripe/developer-docs

default sort on `many_many` relation table isn't clearly documented

Open
#312 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

affects/v4 affects/v5 type/docs
Dominant language
No language data
Stars
7
Forks
74
Avg merge
1d 14m
Merged PRs (30d)
6

Description

Affected Version

5.0.0

Description

I have a BlogPost DataObject that has many_many relation with TaxonomyTerm DataObject through BlogPostTaxonomyTerm as below:

private static array $many_many = [
    'Terms' => [
        'through' => BlogPostTaxonomyTerm::class,
        'from' => 'Parent',
        'to' => 'TaxonomyTerm',
    ],
];

There is a Sort field on the TaxonomyTerm DataObject and a separate Sort field on the many_many through relation DataObject BlogPostTaxonomyTerm.

I want to sort the linked TaxonomyTerms on the BlogPost based on the Sort value on the BlogPostTaxonomyTerm table. However, I am not able to do that. The Sorting result is always determined by the Sort value on the TaxonomyTerm DataObject instead.

Here is an example, I have the following fixture file for unit test:

SilverStripe\Taxonomy\TaxonomyTerm:
  term1:
    Name: term1
    Sort: 1
  term2:
    Name: term2
    Sort: 2
  term3:
    Name: term3
    Sort: 3

App\Pages\BlogPost:
  page1:
    Title: 'Blog Post 1'
    PublishDate: '2023-06-20 15:55:50'

App\Taxonomy\ThroughRelations\BlogPostTaxonomyTerm:
  blogPostTerm1:
    Parent: =>App\Pages\BlogPost.page1
    TaxonomyTerm: =>SilverStripe\Taxonomy\TaxonomyTerm.term3
    Sort: 1
  blogPostTerm2:
    Parent: =>App\Pages\BlogPost.page1
    TaxonomyTerm: =>SilverStripe\Taxonomy\TaxonomyTerm.term2
    Sort: 2
  blogPostTerm3:
    Parent: =>App\Pages\BlogPost.page1
    TaxonomyTerm: =>SilverStripe\Taxonomy\TaxonomyTerm.term1
    Sort: 3

And I want to test the first term of the App\Pages\BlogPost.page1.
The expected result is the first term should be "term3", however the actual first term is "term1".

After I add the following config to the BlogPostTaxonomyTerm many_many relation, the Sorting works as expected.

 private static string $default_sort = '"BlogPostTaxonomyTerm"."Sort"';

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the developer documentation for the many_many relation and default_sort configuration described in the issue. Document that sorting uses the related TaxonomyTerm by default and explain how to configure sorting on the through relation, using the provided BlogPostTaxonomyTerm example. Done means a newcomer can find and understand the required configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.