Skip to content

5.1 Visited State Model

alfmel edited this page Nov 18, 2013 · 7 revisions

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.

View lib/Models/VisitedStateBase.php

View lib/Models/VisitedState.php

The PDO model is a bit more complex.

View lib/Models/VisitedStatePdo.php

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:

  1. queryVisitors($state_id) - Get users that have visited the given state
  2. queryVisitorCount($state_id) - Get the number of visitors to the given state
  3. 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

Testing the Visited State Model

As we have done in the past, we build a unit test to test the functionality of our PDO model.

View unit_tests/VisitedStatePdoModelTest.php

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

Clone this wiki locally