Return to Snippet

Revision: 37047
at December 3, 2010 03:05 by nikefido


Initial Code
<?php
//Abstract "model" class
class App_Model_Abstract
{
	
	public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }
	
	public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid note property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid note property');
        }
        return $this->$method();
    }
	
	public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
}

//Table class
class App_Model_Db_User extends Zend_Db_Table_Abstract
{
	/** Table name */
    protected $_name    = 'users';
    protected $_primary = 'id';
}

//User model class - The class most interfaced with in application code
class App_Model_User extends App_Model_Abstract {
	
	protected $_id;
	protected $_email;
	protected $_date_added;
	protected $_active;
	protected $_user_type;
	
	protected $_mapper;
	
	protected $_has_company;
	protected $_companies = array();
	
	const USER_TYPE_USER = 'user';
	const USER_TYPE_ADMIN = 'admin';
	
	/*
		GETTERS / SETTERS
	*/
	
	public function setId($id) {
		$this->_id = $id;
		return $this;
	}
	
	public function getId() {
		return $this->_id;
	}
	
	public function setEmail($email) {
		$this->_email = substr($email, 0, 120);
		return $this;
	}
	
	public function getEmail() {
		return $this->_email;
	}
	
	public function setDateAdded($last) {
		$this->_date_added = $last;
		return $this;
	}
	
	public function getDateAdded() {
		return $this->_date_added;
	}
	
	public function setActive($active) {
		if($active) {
			$this->_active = 1;
		} else {
			$this->_active = 0;
		}
		return $this;
	}
	
	public function getActive() {
		return $this->_active;
	}
	
	public function setUserType($type) {
		$this->_user_type = substr($type, 0, 10);
		return $this;
	}
	
	public function getUserType() {
		return $this->_user_type;
	}
	
	
	public function setMapper($mapper) {
		$this->_mapper = $mapper;
		return $this;
	}
	
    public function getMapper() {
		if (null === $this->_mapper) {
			$this->setMapper(new App_Model_Mappers_User());
		}
		return $this->_mapper;
    }
    
    /*
    	BASIC DATA
    */
	public function save() {
		$id = $this->getMapper()->save($this);
		$this->setFbId($id);
		return $id;
	}
	
	public function update() {
		return $this->getMapper()->update($this);
	}
	
	public function find($id) {
		if(!$this->getMapper()->find($id, $this)) {
			return false;
		} 
		return $this;
	}
}

//Data mapper (does heavy lifting of this example)
class App_Model_Mappers_User {
	
	protected $_dbTable;
	const TABLE = 'App_Model_Db_User';
	
	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable(self::TABLE);
		}
		return $this->_dbTable;
	}

	public function save(App_Model_User $obj) {
		$data = array(
			'id' => $obj->getId(),
			'email' => $obj->getEmail(),
			'date_added' => $obj->getDateAdded(),
			'active' => $obj->getActive(),
			'user_type' => $obj->getUserType()
		);

		return $this->getDbTable()->insert($data);
	}

	public function update(App_Model_User $obj) {
		$data = array(
			'id' => $obj->getId(),
			'email' => $obj->getEmail(),
			'date_added' => $obj->getDateAdded(),
			'active' => $obj->getActive(),
			'user_type' => $obj->getUserType()
		);			
		$where = $this->getDbTable()->getAdapter()->quoteInto('`id` = ?', $obj->getId());
		return $this->getDbTable()->update($data, $where);
	}

	public function find($id, App_Model_User $obj) {
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return false;
		}
		$row = $result->current();
		$obj->setId($row->id)
			->setEmail($row->email)
			->setDateAdded($row->date_added)
			->setActive($row->active)
			->setUserType($row->user_type);
		
		return $obj;
	}
}

Initial URL

                                

Initial Description
Example set of files for data mapping objects to db records. (Not perfect, but funcional)

Initial Title
Zend Data Mapping example

Initial Tags
data

Initial Language
PHP