Recommend this page to a friend! |
![]() ![]() |
Info | ![]() |
![]() ![]() |
Reputation | Support forum (1) | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2021-02-06 (28 days ago) ![]() | Not enough user ratings | Total: 497 | All time: 5,773 This week: 304![]() |
Version | License | PHP version | Categories | |||
php-identity-map 3 | BSD License | 5.0 | PHP 5, Databases, Design Patterns |
Description | Author | |
This package can store and retrieve objects in persistent storage containers avoiding to have multiple instances of the same object in memory. |
Building an Identity Map in PHP =============================== Sample application used for training. This example code is no production code and should be used for training purposes only. This example code requires: --------------------------- * PDO a lightweight, consistent interface for accessing databases in PHP. * PHPUnit a unit testing framework for PHP projects. This example code implements: ----------------------- * Data-Mapper Pattern * Identity-Map Pattern Why identity mapping? --------------------- By using Data-Mapper pattern without an identity map, you can easily run into problems because you may have more than one object that references the same domain entity. Data-Mapper without identity map ---------------------------------- $userMapper = new UserMapper($pdo); $user1 = $userMapper->find(1); // creates new object $user2 = $userMapper->find(1); // creates new object echo $user1->getNickname(); // joe123 echo $user2->getNickname(); // joe123 $user1->setNickname('bob78'); echo $user1->getNickname(); // bob78 echo $user2->getNickname(); // joe123 -> ?!? Data-Mapper with identity map ---------------------------------- The identity map solves this problem by acting as a registry for all loaded domain instances. $userMapper = new UserMapper($pdo); $user1 = $userMapper->find(1); // creates new object $user2 = $userMapper->find(1); // returns same object echo $user1->getNickname(); // joe123 echo $user2->getNickname(); // joe123 $user1->setNickname('bob78'); echo $user1->getNickname(); // bob78 echo $user2->getNickname(); // bob78 -> yes, much better By using an identity map you can be confident that your domain entity is shared throughout your application for the duration of the request. Note that using an identity map is not the same as adding a cache layer to your mappers. Although caching is useful and encouraged, it can still produce duplicate objects for the same domain entity. Load the Data-Mappers with the Repository class ----------------------------------------------- $repository = new Repository($this->db); $userMapper = $repository->load('User'); $insertId = $userMapper->insert(new User('billy', 'gatter')); |
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
||||
![]() |
Class | autoloder | ||
![]() ![]() |
Data | phpunit configuration | ||
![]() ![]() |
Doc. | README | ||
![]() ![]() |
Aux. | bootstraping |
![]() |
/ | database |
File | Role | Description |
---|---|---|
![]() ![]() |
Data | Auxiliary data |
![]() ![]() |
Data | Auxiliary data |
![]() |
/ | src |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
||||
![]() |
Class | Class source |
![]() |
/ | src | / | framework |
File | Role | Description |
---|---|---|
![]() |
Class | the indentity-map |
![]() |
Class | mapper exception |
![]() |
Class | Class source |
![]() |
/ | src | / | model |
File | Role | Description |
---|---|---|
![]() |
Class | Class source |
![]() |
Class | the user model |
![]() |
/ | src | / | persistence |
File | Role | Description |
---|---|---|
![]() |
Class | Class source |
![]() |
Class | Class source |
![]() |
Class | the user mapper |
![]() |
/ | tests |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
Class | Class source |
![]() |
/ | tests | / | model |
File | Role | Description |
---|---|---|
![]() |
Class | Class source |
![]() ![]() |
Test | user model test |
![]() |
/ | tests | / | persistence |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
Class | Class source | ||
![]() ![]() |
Test | UserMapperTest |
![]() |
/ | tests | / | persistence | / | fixture |
File | Role | Description |
---|---|---|
![]() ![]() |
Data | Auxiliary data |
![]() ![]() |
Data | user-seed |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Comments (1) | |||||
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Related pages |
Retrieve objects avoiding multiple instances Retrieve objects avoiding multiple instances |
php-identity-map on github php-identity-map on github |