You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
Note that the problem here seems to be that private static string $default_sort = 'Sort'; on the join class does not sort by the Sort field on the join class, but rather by the Sort field on the taxonomy term. To sort by the field on the join class, you need to add the table name as part of the sort config.
It will be worth checking whether adding the table name is always required, or if it's only required when there is a conflict of field names.
Most likely this is caused by the way the query is declared, where you're more-or-less doing SELECT "Sort" FROM "TaxonomyTerm" LEFT JOIN "BlogPostTaxonomyTerm" ORDER BY "Sort" - i.e. the Sort field with no alias is from the taxonomy term class, so that's what it sorts by - the ManyManyThroughList isn't taking the join's alias into account when using it to sort by.
The problem then becomes - how can we determine what the developer wants to do - they might want to sort by the Sort field on the taxonomy term itself. So... maybe this is already behaving as expected and just needs to be much more clearly communicated through documentation and PHPDoc comments.
In fact I think I've just convinced myself that _assuming private static string $default_sort = 'Sort'; on the join record does sort by the sort field on taxonomy term, this is purely a documentation problem. I'll move it to the docs repo.
GuySartorelli
changed the title
Sort on many_many relation table doesn't work
default sort on many_many relation table isn't clearly documented
Aug 10, 2023
Affected Version
5.0.0
Description
I have a
BlogPost
DataObject that hasmany_many
relation withTaxonomyTerm
DataObject throughBlogPostTaxonomyTerm
as below:There is a
Sort
field on theTaxonomyTerm
DataObject and a separateSort
field on themany_many
through relation DataObjectBlogPostTaxonomyTerm
.I want to sort the linked
TaxonomyTerms
on theBlogPost
based on theSort
value on theBlogPostTaxonomyTerm
table. However, I am not able to do that. The Sorting result is always determined by theSort
value on theTaxonomyTerm
DataObject instead.Here is an example, I have the following fixture file for unit test:
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.The text was updated successfully, but these errors were encountered: