Created
May 16, 2012 15:31
-
-
Save sandfox/2711318 to your computer and use it in GitHub Desktop.
Simple Model - Mapper abstract pair for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sandfox\Model; | |
use \Sandfox\Model\ModelAbstract; | |
class Cat extends ModelAbstract | |
{ | |
/** | |
* Define data names and defaults here | |
* @var array | |
*/ | |
protected $data = array( | |
'id' => null, | |
'food' => null, | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sandfox\Mapper; | |
use \Sandfox\Model\Cat as CatModel; | |
use \Sandfox\Mapper\MapperAbstract; | |
use \Sandfox\Model\ModelAbstract; | |
class Cat extends MapperAbstract | |
{ | |
protected $modelClass = '\Sandfox\Model\Cat'; | |
protected $tableName = 'cats'; | |
protected function hydrate(\Zend_Db_Table_Row $row) | |
{ | |
$cat = new $this->modelClass; | |
$cat->food = $row->food; | |
$cat->id = $row->cat_id; | |
return $subscriber; | |
} | |
protected function dehydrate(ModelAbstract $model) | |
{ | |
$data = array(); | |
$data['food'] = $model->food; | |
$data['cat_id'] = $model->id; | |
return $data; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sandfox\Mapper; | |
use \Sandfox\Model\ModelAbstract; | |
/** | |
* Abstract class to handle the generics of our mappers | |
* FIXME (everything) | |
*/ | |
abstract class MapperAbstract | |
{ | |
protected $modelClass = '\Sandfox\Model\ModelAbstract'; | |
protected $tableGateway = null; | |
protected $tableName = null; | |
protected $identityMap = array(); | |
public function __construct(\Zend_Db_Table_Abstract $tableGateway = null) | |
{ | |
if($tableGateway === null) { | |
$this->tableGateway = new \Zend_Db_Table($this->tableName); | |
} else { | |
$this->tableGateway = $tableGateway; | |
} | |
} | |
protected function getGateway() | |
{ | |
return $this->tableGateway; | |
} | |
public function getById($id) | |
{ | |
if ($this->hasIdentity($id)) { | |
return $this->getIdentity($id); | |
} | |
$record = $this->getGateway()->find($id)->current(); | |
$model = $this->hydrate($record); | |
$this->setIdentity($model->id, $model); | |
return $model; | |
} | |
protected function getIdentity($id) | |
{ | |
if ($this->hasIdentity($id)) { | |
return $this->identityMap[$id]; | |
} | |
} | |
protected function setIdentity($id, $model) | |
{ | |
$this->identityMap[$id] = $model; | |
} | |
protected function hasIdentity($id) | |
{ | |
return array_key_exists($id, $this->identityMap); | |
} | |
/** | |
* Hydrates the object model with the data from the persistence layer | |
* @param array $data [description] | |
* @return \Marlow\Model\ModelAbstract [description] | |
*/ | |
abstract protected function hydrate(\Zend_Db_Table_Row $row); | |
/** | |
* Turns the model into a simple array/whatever to be fed in persistence layer | |
* @param ModelAbstract $model [description] | |
* @return [type] [description] | |
*/ | |
abstract protected function dehydrate(ModelAbstract $model); | |
public function save(ModelAbstract $model) | |
{ | |
$data = $this->dehydrate($model); | |
if (!isset($model->id)) { | |
$model->id = $this->getGateway()->insert($data); | |
$this->setIdentity($model->id, $model); | |
return $model->id; | |
} else { | |
$where = $this->getGateway()->getAdapter()->quoteInto('id = ?', $author->id); | |
return $this->getGateway()->insert($data, $where); | |
} | |
} | |
public function delete(ModelAbstract $model) | |
{ | |
$where = $this->getGateway()->getAdapter()->quoteInto('id = ?', $entry->id); | |
$this->getGateway()->delete($where); | |
} | |
public function update(ModelAbstract $model) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sandfox\Model | |
abstract class ModelAbstract | |
{ | |
protected $data = array(); | |
public function construct(array $data = null) | |
{ | |
foreach ($data as $name => $value) { | |
//Do I need the curly braces? | |
$this->{$name} = $value; | |
} | |
} | |
public function toArray() | |
{ | |
return $this->data; | |
} | |
public function __set($name, $value) | |
{ | |
if (!array_key_exists($name, $this->data)) { | |
throw new \Exception("Attempting to set dynamically set non-existent property $name"); | |
} | |
$this->data[$name] = $value; | |
} | |
public function __get($name) | |
{ | |
if (array_key_exists($name, $this->data)) { | |
return $this->data[$name]; | |
} | |
} | |
public function __isset($name) | |
{ | |
return isset($this->data[$name]); | |
} | |
public function __unset($name) | |
{ | |
if (isset($this->data[$name])) { | |
unset($this->data[$name]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment