Recommend this page to a friend! |
Download |
Info | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-01-09 (9 months ago) | 52% | Total: 503 | All time: 5,803 This week: 129 |
Version | License | PHP version | Categories | |||
php-identity-map 16 | 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. |
Sample application used for training.
This example code is no production code and should be used for training purposes only.
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.
$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 -> ?!?
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.
$repository = new Repository($this->db);
$userMapper = $repository->load('User');
$insertId = $userMapper->insert(new User('billy', 'gatter'));
Files (22) |
File | Role | Description | ||
---|---|---|---|---|
database (2 files) | ||||
src (1 file, 3 directories) | ||||
tests (1 file, 2 directories) | ||||
autoload.php | Class | autoloder | ||
phpunit.xml.dist | Data | phpunit configuration | ||
README.markdown | Doc. | README | ||
test-bootstrap.php | Aux. | bootstraping |
Files (22) | / | database |
File | Role | Description |
---|---|---|
tbl_article.sql | Data | Auxiliary data |
tbl_user.sql | Data | Auxiliary data |
Files (22) | / | src |
File | Role | Description | ||
---|---|---|---|---|
framework (3 files) | ||||
model (2 files) | ||||
persistence (3 files) | ||||
Repository.php | Class | Class source |
Files (22) | / | src | / | framework |
File | Role | Description |
---|---|---|
IdentityMap.php | Class | the indentity-map |
MapperException.php | Class | mapper exception |
RecursiveClassLoder.php | Class | Class source |
Files (22) | / | src | / | model |
File | Role | Description |
---|---|---|
Article.php | Class | Class source |
User.php | Class | the user model |
Files (22) | / | src | / | persistence |
File | Role | Description |
---|---|---|
AbstractMapper.php | Class | Class source |
ArticleMapper.php | Class | Class source |
UserMapper.php | Class | the user mapper |
Files (22) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
model (2 files) | ||||
persistence (2 files, 1 directory) | ||||
RepositoryTest.php | Class | Class source |
Files (22) | / | tests | / | model |
File | Role | Description |
---|---|---|
ArticleTest.php | Class | Class source |
UserTest.php | Test | user model test |
Files (22) | / | tests | / | persistence |
File | Role | Description | ||
---|---|---|---|---|
fixture (2 files) | ||||
ArticleMapperTest.php | Class | Class source | ||
UserMapperTest.php | Test | UserMapperTest |
Files (22) | / | tests | / | persistence | / | fixture |
File | Role | Description |
---|---|---|
article-seed.xml | Data | Auxiliary data |
user-seed.xml | Data | user-seed |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | 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 |
php-identity-map on github |