Feature Implementation Plan: Add Sort Order to Species and Processing Methods¶
📋 Todo Checklist¶
- [ ] Add a
sortOrderfield to theSpeciesentity. - [ ] Add a
sortOrderfield to theProcessingMethodentity. - [ ] Create a database migration for the schema changes.
- [ ] Update the
SpeciesRepositoryto sort by the new field. - [ ] Update the
ProcessingMethodRepositoryto sort by the new field. - [ ] Update the admin CRUD controllers for both entities to allow managing the
sortOrder. - [ ] Write a data migration or fixture to populate the initial sort order values.
- [ ] Final Review and Testing
🔍 Analysis & Investigation¶
Codebase Structure¶
- Entities: The
src/Entity/Species.phpandsrc/Entity/ProcessingMethod.phpfiles will be modified. - Repositories: The
src/Repository/SpeciesRepository.phpandsrc/Repository/ProcessingMethodRepository.phpwill be updated to change their default sorting. - Admin: The corresponding CRUD controllers in
src/Controller/Admin/will be updated to make the new field editable.
Current Architecture & Problem¶
- Problem: The API endpoints for
SpeciesandProcessingMethodreturn lists sorted alphabetically by name. This is not always the desired order for presentation in a user interface. - Solution: This plan follows the pattern already established by the
FlavorWheelNodeentity. By adding a nullable integersortOrderfield to bothSpeciesandProcessingMethod, we can explicitly control their display order. The API will then return the items pre-sorted, simplifying frontend logic.
Dependencies & Integration Points¶
- Doctrine: Will be used to add the new column to the database tables and to update the
ORDER BYclauses in the repository queries. - EasyAdminBundle: The admin interface will be updated using a
NumberFieldto allow easy management of the sort order.
Considerations & Challenges¶
- Data Population: After adding the
sortOrdercolumn, existing records will have aNULLvalue. A data migration or a fixture is required to populate these fields with a sensible default order.NULLvalues should be sorted last. - Admin UI: The
sortOrderfield should be added to the list view in the admin panel to make it easy to see and manage the current order.
📝 Implementation Plan¶
Prerequisites¶
- No new external dependencies are required.
Step-by-Step Implementation¶
-
Update the Entities
- Files to modify:
src/Entity/Species.php,src/Entity/ProcessingMethod.php - Changes needed for both entities:
- Add a new private property:
#[ORM\Column(nullable: true)] private ?int $sortOrder = null; - Add an index for performance:
#[ORM\Index(columns: ['sort_order'])](at the class level). - Add the appropriate serialization group:
#[Groups(['...'])](e.g.,species:read,processing:read). - Add a public getter and setter for the new property.
- Add a new private property:
- Files to modify:
-
Create Database Migration
- Action: Run
symfony console make:migrationto generate a new migration file. - Changes needed: Verify that the generated migration correctly adds the
sort_ordercolumn and an index to both thespeciesandprocessing_methodtables.
- Action: Run
-
Update Repositories to Use New Sorting
- Files to modify:
src/Repository/SpeciesRepository.php,src/Repository/ProcessingMethodRepository.php SpeciesRepository.phpChanges:- In the
findByFiltersmethod (and any other list-finding methods), change theorderByclause from's.commonName'to's.sortOrder'. UseASCand handleNULLs by sorting them last (ORDER BY s.sortOrder ASC NULLS LAST).
- In the
ProcessingMethodRepository.phpChanges:- In
findAllHierarchicalandfindByFiltersWithPagination, change theorderByclause from'p.name'to'p.sortOrder'.
- In
- Files to modify:
-
Update Admin CRUD Controllers
- Files to modify:
src/Controller/Admin/SpeciesCrudController.php,src/Controller/Admin/ProcessingMethodCrudController.php - Changes needed for both controllers:
- In the
configureFieldsmethod, add aNumberFieldforsortOrder. - Add the
sortOrderfield to the index page view as well, so it's visible in the list.
- In the
- Files to modify:
-
Populate Initial Sort Order Data
- Action: Create a new Doctrine data fixture (
src/DataFixtures/SortOrderFixture.php). - Changes needed:
- The fixture should load all
SpeciesandProcessingMethodentities. - It should iterate through them and assign a default
sortOrderbased on a predefined list or alphabetically. For example, forSpecies, you might want "Arabica" to be1, "Canephora (Robusta)" to be2, etc. - Run the fixture to populate the database.
- The fixture should load all
- Action: Create a new Doctrine data fixture (
Testing Strategy¶
- Unit Tests:
- Update unit tests for the repositories to ensure their query builders are now sorting by
sortOrder.
- Update unit tests for the repositories to ensure their query builders are now sorting by
- Integration Tests:
- Update the integration tests for the
/api/speciesand/api/processing-methodsendpoints. - Before the test, use fixtures or direct entity manipulation to set a specific
sortOrderfor a few items. - Call the API endpoint and assert that the items are returned in the exact order you specified, not alphabetically.
- Update the integration tests for the
🎯 Success Criteria¶
- The
SpeciesandProcessingMethodentities have a functionalsortOrderfield. - The API endpoints for listing these entities now return them sorted by the
sortOrderfield by default. - The admin panel allows for easy viewing and editing of the
sortOrderfor both entity types. - All existing records have a default
sortOrdervalue populated. - The new sorting logic is covered by tests.