Working With Mappers
Since Spot follows the DataMapper design
pattern, you will need a
mapper instance for working with object Entities and database tables. You can
get a mapper instance from the
mapper method by
providing the fully qualified entity namespace + class name:
Mappers only work with one entity type, so you will need one mapper per entity class you work with (i.e. to save an Entity\Post, you will need the appropriate mapper, and to save an Entity\Comment, you will need a comment mapper, not the same post mapper. Relations will automatically be loaded and handled by their corresponding mapper by Spot.
Using Custom Mappers
Although you do not have to create a mapper for each entity, sometimes it is nice to create one if you have a lot of custom finder methods, or want a better place to contain the logic of building all the queries you need.
Just specify the full mapper class name in your entity:
And then create your mapper:
Then when you load the mapper like normal, Spot will see the custom
Entity\Post::$mapper you defined, and load that instead of the generic one,
allowing you to call your custom method: