Home arrow PHP arrow Roll Your Own Repository in PHP: a More Functional User Repository

Roll Your Own Repository in PHP: a More Functional User Repository

In this penultimate installment of the series, I extend the functionality of the user repository created in the previous part by adding two methods to it. These methods will handle adding new user entities to the underlying MySQL database and removing existing ones. Their implementation, as you'll see, will be a fairly straightforward process.

  1. Roll Your Own Repository in PHP: a More Functional User Repository
  2. Adding and removing user entities
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 13, 2010

print this article



If youíre a PHP developer interested in expanding your current background in design patterns, and you want more intimate knowledge of building a repository from scratch, then this is the article series for you. Over the course of its eight tutorials, youíll be guided through the development of a sample web application, whose main area of responsibility will be to query collections of user entities via a simple, yet functional repository class.

And now that you've been introduced to the seriesí primary goal, itís time to review the topics covered in the last installment. In that part I defined the initial implementation of the aforementioned repository. This implementation could retrieve, from the underlying MySQL database, collections of user objects that matched some specified criteria. In addition, I defined a basic factory class that allowed you to hide from client code all the complexities involved in the creation of the repository. This process helped to isolate the object instantiation from the application's logic.

Itís valid to note, however, that this sample user repository still isnít capable of inserting and deleting user entities from the persistence layer, so its current functionality is somewhat limited. In this penultimate part of the series, however, Iím going to add to the repository class a couple of simple methods that will be tasked with performing the insertions and deletions in a fairly simple way.

Do you want to learn how these additional methods will be defined? Then jump ahead and read the following lines!

Review: the current user repository class

Since I intend to extend the functionality of the user repository class created in the previous part of the series, itíd be instructive to recall the initial definition of this class. You'll see how it can be used for fetching collections of user entities through a set of intuitive methods.

With that said, hereís the partial definition of this sample repository. Right now it's only able to retrieve user entities according to a given first and last name, and by specifying an email address:



class UserRepository
    protected $_userMapper;
     * Class constructor
    public function __construct(UserMapper $userMapper)
        $this->_userMapper = $userMapper;   
     * find users by their first names
    public function findByFirstName($fname)
        return $this->_userMapper->search("fname = '$fname'");
     * find users by their last names
    public function findByLastName($lname)
        return $this->_userMapper->search("lname = '$lname'");
     * find users by their email addresses
    public function findByEmail($email)
        return $this->_userMapper->search("email = '$email'");

If you ever thought that the construction of a repository class was a challenging process, the above code sample should change your mind quickly. As you can see, the only dependency injected into the internals of this class is an instance of the user mapper. This is used internally for each finder, to retrieve collections of user entities that match some basic conditions.

Since the creation of this repository requires the instantiation of all of its collaborators, I decided to hide the complexities of this process inside a factory. Its definition looks like this:    



class RepositoryFactory
     * Create a repository based on the given name
    public function create($name, array $options = array())
        $name = ucfirst(strtolower($name));
        $adapter    = MySQLAdapter::getInstance();
        $collection = $name . 'Collection';
        $mapper     = $name . 'Mapper';
        $repository = $name . 'Repository';
        return new $repository(
            new $mapper($adapter, new $collection, $options));

Effectively, the above factory does the hard work and takes care of correctly instantiating different kinds of repositories, including the one that works with user entities. Since this class behaves like a simple  dependency injection container, itís time to provide the user repository with the ability to add and delete user objects.

As I stated in the introduction, this extended functionality will be implemented through a few additional methods. These methods will be discussed in depth in the following section. Thus, to learn more about how these methods will be defined, click on the link below and read the lines to come.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: