-- tridem-zend <[email protected]> wrote
(on Wednesday, 23 March 2011, 01:27 AM -0700):
> Got them, Matthew! I tested two servers but both from the same provider and
> the ports were indeed blocked!
> Now it woks like a charme.
> 
> I want to switch to Doctrine 2.0 soon. At the moment I use the
> Model->Mapper->DbTable chain to connect to a regular MySQL database.
> 
> What is the best way to add Phly_Couch to this chain? Make the Mapper extend
> it or the DbTable?

Typically a mapper doesn't extend the data access, but instead composes
it. As such, you will typically have an interface for data access for
common operations -- fetch, create, update, delete, and possibly
querying -- and wrap your actual access layer (couch, RDBMS, etc.) in an
implementation. The mapper then calls only methods defined in the
interface, and handles mapping your domain objects to input for the data
access layer, and output from the data access layer to domain objects.

As a quick example:

    interface DataAccess
    {
        public function fetch($identifier);
        public function create(array $data);
        public function update($identifier, array $data);
        public function delete($identifier);
    }

    class CouchDataAccess implements DataAccess
    {
        protected $couchdb;

        public function __construct(CouchDb $couch)
        {
            $this->couchdb = $couch;
        }

        public function fetch($identifier)
        {
            return $this->couchdb->docOpen($identifier);
        }

        public function create(array $data)
        {
            $result = $this->couchdb->docSave($data);
            return $this->couchdb->docOpen($result->id);
        }

        public function update($identifier, array $data)
        {
            $result = $this->couchdb->docSave($data, $identifier);
            return $this->couchdb->docOpen($result->id);
        }

        public function delete($identifier)
        {
            $this->couchdb->docRemove($identifier);
        }
    }

    class Mapper
    {
        protected $dataAccess;
        protected $entityClass;

        public function __construct(DataAccess $dataAccess, $entityClass)
        {
            $this->dataAccess  = $dataAccess;
            $this->entityClass = $entityClass;
        }

        public function create(Entity $entity)
        {
            $data = $this->dataAccess->create($entity->toArray());
            $entity->fromArray($data);
            return $entity;
        }

        // ...
    }

-- 
Matthew Weier O'Phinney
Project Lead            | [email protected]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to