Skip to content

Instantly share code, notes, and snippets.

@andreyp
Created January 13, 2011 16:50
Show Gist options
  • Save andreyp/778161 to your computer and use it in GitHub Desktop.
Save andreyp/778161 to your computer and use it in GitHub Desktop.
<?php
class Model{
protected $id = null;
public function setId($id){
$this->id = $id;
}
public function getId(){
return $this->id;
}
}
class DataMapper{
protected $cache_namespace = '';
// Тут общая логика
public function findById($id){
if(!$data = $this->getCache($this->cache_namespace,array('id'=>$id))){
// тут грузим из БД
}
return $this->loadData($data);
}
public function save($object){
// тут сохраняем объект, чистим кэш, и т.д.
}
abstract function loadData($data);
}
class PlaceMapper extends DataMapper{
protected $cache_namespace = 'place';
public function findByUserId($user_id){
// в каждом методе решаем а нужно ли нам идти в БД и Sphinx или кэш
}
public function findByTitle($title){
// к примеру тут мы грузим только из sphinx'a
}
protected function loadData($data){
// тут наполняем объект
}
}
class Place extends Model{
private $title = '';
public function setTitle($title){
$this->title = $title;
}
public function getTitle(){
return $this->title;
}
}
class User extends Model{
private $email = '';
private $places = null;
public function setEmail($email){
$this->email = $email;
}
public function getPlaces(){
if($this->places === null){
$placeMapper = new PlaceMapper();
$this->places = $placeMapper->FindByUserId($this->getId());
}
}
}
// Используем
$userMapper = new UserMapper();
$user = $userMapper->findById(1);
$user->getPlaces();
$user->setEmail('[email protected]');
$userMapper->save($user);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment