-
Notifications
You must be signed in to change notification settings - Fork 1
5.1 Visited State Model
Go back to 5. Visited States
To provide the states visited functionality, we begin by creating a model based on the VisitedState table. This table contains two fields that create a many-to-many join from the User and State tables.
- Email (text)
- StateID (text)
The base and standard models are straightforward at this point.
The PDO model is a bit more complex.
This model is a bit different since we allow multiple types of queries to be done against it. Rather than creating multiple models for each type of query, we simply add query methods. In this case we add three additional methods:
- queryVisitors($state_id) - Get users that have visited the given state
- queryVisitorCount($state_id) - Get the number of visitors to the given state
- queryVisited($user_id) - Get the list of states a user has visited
Each of these methods creates its own queries and performs the necessary caching and fetching of records.
Something to note in these methods is that they query the security context directly. That's because queries don't go through the authorization() method. It also shows how security decisions can be made anywhere in the code since the security context is available throughout most of the classes.
Something else to note in the PDO model is that we only initially allow Read and Query operations (line 15). The insert and delete operations are entirely controlled by the Visited State Authorization Provider.
View lib/Security/Authorization/VisitedStateModelAuthorizationProvider.php
As we have done in the past, we build a unit test to test the functionality of our PDO model.
For this test we use use another in-memory SQLite database and add base data during the test setUp(). Notice our dummy dataset contains both states, users and visited states.
Continue to 5.2 Visited State Class