Home arrow PHP arrow Page 2 - Roll Your Own Repository in PHP: a Final Example

The user repository in action - PHP

Welcome to the conclusion to an eight-part article series on constructing your own repository from scratch in PHP. In this article, we'll go through an example that shows you how the repository we completed in the previous article actually performs. You'll see all of the classes we created in action.

TABLE OF CONTENTS:
  1. Roll Your Own Repository in PHP: a Final Example
  2. The user repository in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate how to take advantage of the functionality offered by the user repository that you saw in the previous section, I'm going to use a simple MySQL table, which has been previously populated with data corresponding to a few trivial users. The sample table looks like this:

So far, nothing unexpected, right? As you can see, the table contains four columns that store the ID (an autonumeric, unsigned primary key) of each user, as well as their first and last names and their email addresses. Now, if I wanted to retrieve some of the stored users by specifying some basic conditions, the process would be this simple:   

<?php

// include the Autoloader
require_once 'Autoloader.php';
Autoloader::getInstance();

// create an instance of the Repository factory
$repositoryFactory = new RepositoryFactory;

// create a user repository
$userRepository = $repositoryFactory->create('user');

// fetch users by their first names
$users = $userRepository->findByFirstName('Alex');
foreach ($users as $user) {
    echo $user->fname . ' ' . $user->lname . ' ' . $user->email . '<br />';
}
/* displays the following
Alex Gervasio alex@domain.com
*/

// fetch users by their last names
$users = $userRepository->findByLastName('Wilson');
foreach ($users as $user) {
    echo $user->fname . ' ' . $user->lname . ' ' . $user->email . '<br />';
}
/* displays the following
Sandra Wilson sandra@domain.com
Jennifer Wilson jennifer@domain.com
*/

// fetch users by their email addresses
$users = $userRepository->findByEmail('susan@domain.com');
foreach ($users as $user) {
    echo $user->fname . ' ' . $user->lname . ' ' . $user->email . '<br />';
}
/* displays the following
Susan Norton susan@domain.com
Susan Hamilton susan@domain.com
*/

As the above code fragment shows, putting the user repository into action and fetching different collections of user entities that match a specific condition is amazingly easy! In this case, the criteria used to retrieve the previous users are very simple. It's feasible, however, to create a repository capable of working with more complex conditions than simple first and last names. The possibilities are endless. 

Naturally, the repository can be used to insert new users into the above table, and even to remove existing users through the same API. Therefore, in the last section I'm going to build a final example that will show how to perform these additional operations.

Now, jump ahead and keep reading.  

One step further: adding and deleting user entities

As I noted in the section that you just read, the user repository makes it really easy to add new users to the persistence layer, and to delete existing ones. If you want to see how these operations can be executed in a few steps, simply pay attention to the code snippet below, which will hopefully be self-explanatory:

<?php

// include the Autoloader
require_once 'Autoloader.php';
Autoloader::getInstance();

// create an instance of the Repository factory
$repositoryFactory = new RepositoryFactory;

// create a user repository
$userRepository = $repositoryFactory->create('user');

// insert a new user
$user = new User(array('fname' => 'Mary', 'lname' => 'Anderson', 'email' => 'mary@domain.com'));
$userRepository->insert($user);

// delete an existing user
$userRepository->delete(1);

There you have it. Thanks to the functionality provided by the repository's "insert()" and "delete()" methods, inserting and deleting users from the persistence layer is indeed a straightforward process that can be tackled in a snap.

Needless to say, there's plenty of room to experiment here, and extend the current capability of the repository in such a way that it can perform more complex queries. Considering this scenario, I encourage you to tweak this class in many clever ways and add your own custom methods to it. In doing so, you'll acquire a better background in mastering one of the big pillars of Domain-Driven Design.   

Final thoughts

Finally, we've come to the end of this series. From a strong hands-on standpoint, I attempted to demonstrated in a step-by-step fashion how to build a repository from scratch. Hopefully, during the development process, you learned not only how to create such an abstraction layer, but how to apply some concepts inherent to modern application design, such as building models that are loosely coupled to the persistence mechanism.

If you feel that the implementation of Active Record, widely promulgated by Rails and even other well-known PHP frameworks out there, doesn't suit your needs any longer and want to create programs that scale up well in different conditions and with multiple infrastructures, then this series might help you start migrating to more flexible solutions.

See you in the next PHP development tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: