Home arrow MySQL arrow Page 2 - Building an ORM in PHP: Domain Modeling

Handling Collections of Entities - MySQL

If you feel that the features provided by popular ORM packages like Doctrine 2.x or RedBeanPHP are rather like overkill for handling the relationships between the domain objects that comprise your applications, then take a peek at this tutorial. In a step-by-step fashion, youíll be guided through the development of a simple, extendable ORM, which youíll be able to tweak to suit your personal requirements and needs.

TABLE OF CONTENTS:
  1. Building an ORM in PHP: Domain Modeling
  2. Handling Collections of Entities
By: Alejandro Gervasio
Rating: starstarstarstarstar / 0
November 22, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In fact, providing the previous domain model with a structure capable of traversing, counting and accessing entities is a straightforward process. Not convinced? Then take a look at the following code bit, which hopefully will make your doubts vanish:

(Blog/Model/Collection/CollectionInterface.php)

<?php

namespace BlogModelCollection;
use BlogModel;

interface CollectionInterface extends Countable, IteratorAggregate, ArrayAccess
{
    public function toArray();

    public function clear();

    public function reset();

    public function add($key, ModelAbstractEntity $entity);
   
    public function get($key);

    public function remove($key);

    public function exists($key);
}

 

(Blog/Model/Collection/EntityCollection.php)

<?php

namespace BlogModelCollection;
use BlogModel;

class EntityCollection implements CollectionInterface
{
    protected $_entities = array();

    /**
     * Constructor
     */
    public function  __construct(array $entities = array())
    {
        $this->_entities = $entities;
        $this->reset();
    }

    /**
     * Get the entities stored in the collection
     */
    public function toArray()
    {
        return $this->_entities;
    }

    /**
     * Clear the collection
     */
    public function clear()
    {
        $this->_entities = array();
    }
    
    /**
     * Rewind the collection
     */    
    public function reset()
    {
        reset($this->_entities);
    }

    /**
     * Add an entity to the collection
     */
    public function add($key, ModelAbstractEntity $entity) {
        return $this->offsetSet($key, $entity);
    }

    /**
     * Get from the collection the entity with the specified key
     */
    public function get($key)
    {
        return $this->offsetGet($key);
    }

    /**
     * Remove from the collection the entity with the specified key
     */
    public function remove($key)
    {
        return $this->offsetUnset($key);
    }

    /**
     * Check if the entity with the specified key exists in the collection
     */
    public function exists($key)
    {
        return $this->offsetExists($key);
    }

    /**
     * Count the number of entities in the collection
     */
    public function count()
    {
        return count($this->_entities);
    }

    /**
     * Get the external array iterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->toArray());
    }

    /**
     * Add an entity to the collection
     */
    public function offsetSet($key, $entity)
    {
        if (!$entity instanceof ModelAbstractEntity) {
            throw new InvalidArgumentException('To add an entity to the collection, it must be an instance of AbstractEntity.');
        }
        if (!isset($key)) {
            $this->_entities[] = $entity;
        }
        else {
            $this->_entities[$key] = $entity;
        }
        return true;
    }
   
    /**
     * Remove an entity from the collection
     */
    public function offsetUnset($key)
    {
        if ($key instanceof ModelAbstractEntity) {
            $this->_entities = array_filter($this->_entities, function ($v) use ($key) {
                return $v !== $key;
            });
            return true;
        }
        if (isset($this->_entities[$key])) {
            unset($this->_entities[$key]);
            return true;
        }
        return false;
    }
   
    /**
     * Get the specified entity in the collection
     */
    public function offsetGet($key)
    {
        return isset($this->_entities[$key])
            ? $this->_entities[$key] : null;
    } 
   
    /**
     * Check if the specified entity exists in the collection
     */    
    public function offsetExists($key)
    {
        return isset($this->_entities[$key]);
    }
}

There you have it. Even though my previous statement sounded somewhat exaggerated, the truth is that building a class that manipulates collections of entities only requires the creation of a custom array iterator, which implements a very granular interface.

Of course, itís possible to make the interface even more abstract; to my taste, it reveals too much about what type of object must be passed to its implementers to work as intended. But, for the moment this will be left as homework for you, in case you feel adventurous and want to improve the ORMís base code.

Final Thoughts

Although at first sight it looks like the implementation of the previous domain model hasnít influenced the functionality of this custom ORM at all, this is a misleading impression. Still, if you currently think that way, I donít blame you, because there are still some extra components that must be added to the project to get everything working together in sweet harmony.

In line with this idea, in the final chapter Iíll be incorporating into the ORM the corresponding proxy classes (remember those ones, right?), a simple service layer and some factories. With all of these modules finally working together, youíll see that manipulating entities via one-to-one and one-to-many relationships will be much simpler that you might think.

Donít miss the last part!



 
 
>>> More MySQL Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

MYSQL ARTICLES

- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: